|
-
Mar 23rd, 2011, 09:51 AM
#1
[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.
-
Mar 23rd, 2011, 10:19 AM
#2
Re: LINQ Problem
E.g.
vb.net Code:
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.
-
Mar 23rd, 2011, 12:47 PM
#3
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 :)
-
Mar 24th, 2011, 08:48 AM
#4
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!
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|