[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.
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?
Re: Using LINQ to Count Items in List<T>
Something like this?
csharp Code:
int T = dt.Rows.Count(r => r["ProductID"] == PMTDProductID);
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();
Re: Using LINQ to Count Items in List<T>
Quote:
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.)
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.