Results 1 to 6 of 6

Thread: [RESOLVED] Using LINQ to Count Items in List<T>

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Resolved [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.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    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?

  3. #3
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Using LINQ to Count Items in List<T>

    Something like this?

    csharp Code:
    1. int T = dt.Rows.Count(r => r["ProductID"] == PMTDProductID);
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    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();

  5. #5
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Using LINQ to Count Items in List<T>

    Quote Originally Posted by mojo69 View Post
    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.)
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    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
  •  



Click Here to Expand Forum to Full Width