|
-
Dec 21st, 2012, 11:43 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Using LINQ to Count Items in List<T>
I would like to use LINQ to count how many times there is a match in a List<T>. The count need to tell me how may times that a productid and customerid exist in the List<T>. I have this code that I pieced together from googling this subject.
Code:
var drow = from DataRow myRow in dt.Rows
where myRow["ProductID"].ToString() == PMTDProductID
select myRow;
Can I use this code to get the number of datarows? It is not currently and am not sure what is wrong. This will be the first time that I attempt to use LINQ.
-
Dec 21st, 2012, 12:39 PM
#2
Thread Starter
Fanatic Member
Re: Using LINQ to Count Items in List<T>
Changed my code to use the Find method for the List<T>;
Code:
YPUR yFind = _ypur.Find(delegate(YPUR yr)
{
return yr.ProductID == PMTDProductID;
}
);
I am getting a response that it found it (!null) or it did not (null). Can I use this to get the List<T> index?
-
Dec 22nd, 2012, 03:21 AM
#3
Re: Using LINQ to Count Items in List<T>
Something like this?
csharp Code:
int T = dt.Rows.Count(r => r["ProductID"] == PMTDProductID);
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Dec 26th, 2012, 11:44 AM
#4
Thread Starter
Fanatic Member
Re: Using LINQ to Count Items in List<T>
Code:
var results = from myRow in dt.AsEnumerable()
where myRow.Field<string>("ProductID") == PMTDProductID && myRow.Field<string>("CustNo") == APMFCustNo
select myRow;
int resultcnt = results.Count();
-
Dec 26th, 2012, 07:26 PM
#5
Re: Using LINQ to Count Items in List<T>
 Originally Posted by mojo69
Code:
var results = from myRow in dt.AsEnumerable()
where myRow.Field<string>("ProductID") == PMTDProductID && myRow.Field<string>("CustNo") == APMFCustNo
select myRow;
int resultcnt = results.Count();
Why did you post that? Are you still having issues or this is your solution? I don't get it... You can't just post code as a reply and assume people know what you mean.
As in my example though, you do not need the where clause, and you don't even need the select either. That's a waste of time using those, unless you're going to use the data afterwards. (If you used dot notation though, you would only need to end at where.)
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Dec 27th, 2012, 08:52 AM
#6
Thread Starter
Fanatic Member
Re: [RESOLVED] Using LINQ to Count Items in List<T>
The code I posted is what is working for me right now. I will keep looking for better ways, including your example. The data will be used afterward. I started another thread to handle that. I appreciate you looking at this and apologize for not explaining the reason behind the last post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|