Results 1 to 4 of 4

Thread: [RESOLVED] LINQ Problem

  1. #1

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Resolved [RESOLVED] LINQ Problem

    I got a class object that holds some data: Customer, State, Sales, Date. I then call a database with a large collection of data and populate several thousand of these class objects.

    Next, using a little LINQ, I make two IEnumerable collections of these objects based on the date-range.

    Each of these, I use some more LINQ to Aggregate them into Groups by State and Customer. Thus, now I got two lists, each list has a unique identity based on State and Customer.

    How do I get a list of all items that are in List2 that don't have a matching entry in List1 (matching based on the State/Customer)? I cobbled together a way to do it but I suspect there's a far simpler solution. Also, looking over this code, can anything be simplified? This is my first time doing mass-LINQ manipulation of data objects. I usually don't get this detailed in it.

    Code is below for review.

    Code:
            Dim r1 = From so In Me Where so.DateInvoiced >= dteFrom1 AndAlso so.DateInvoiced <= dteTo1 Select so
            Dim r2 = From so In Me Where so.DateInvoiced >= dteFrom2 AndAlso so.DateInvoiced <= dteTo2 Select so
    
            Dim rUSA1 = From so In r1 Where Not so.Ship_State.Equals(String.Empty) Select so
            Dim rUSA2 = From so In r2 Where Not so.Ship_State.Equals(String.Empty) Select so
    
            Dim decGrandTotal1 As Decimal = Aggregate so In r1 Select so.Total Into Sum()
            Dim decGrandTotal2 As Decimal = Aggregate so In r2 Select so.Total Into Sum()
    
            Dim rStates1 = From so In rUSA1 Group so By so.Ship_State, so.CustomerCode Into Group Select Ship_State, CustomerCode, Total = Group.Sum(Function(so) so.Total), Percent = Group.Sum(Function(so) so.Total) / decGrandTotal1
            Dim rStates2 = From so In rUSA2 Group so By so.Ship_State, so.CustomerCode Into Group Select Ship_State, CustomerCode, Total = Group.Sum(Function(so) so.Total), Percent = Group.Sum(Function(so) so.Total) / decGrandTotal2
    
            'How do I get a list of all rStates2 items that don't have a matching rStates1 record?
            'Can any of the above code be simplified (such as the Total = Group.Sum(Function) parts)?
    Last edited by Jenner; Mar 23rd, 2011 at 09:59 AM.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: LINQ Problem

    E.g.
    vb.net Code:
    1. Dim complement = list2.Where(Function(x) Not list1.Any(Function(y) y.SomeProperty = x.SomeProperty))
    That will get all the items in list1 where there is not an item in list2 with the same SomeProperty value. Where and Any are both extensions of IEnumerable, so those lists can be arrays, collections or the results of other LINQ queries.

  3. #3
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: LINQ Problem

    As for simplifying, the Select so statements at the end of the first 4 lines can be removed. Not so.Ship_State.Equals(String.Empty) can be replaced with so.Ship_State <> String.Empty.
    Last edited by minitech; Mar 24th, 2011 at 10:43 AM. Reason: I wrote != instead of <> by accident :)

  4. #4

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: LINQ Problem

    Perfect! Thanks jmcihinney. That's a nice one to know; I don't think I ever did a construction like that before. It's exactly what I needed to get this sales data cruncher/comparer working.

    Thanks for the tips too minitech! Always interested in ways to shorten/simplify my code!
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Tags for this Thread

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