Compare lists and get values with Linq?

You can do this by the following Linq statement: var result = List1. Where(x =>! List2.

Select(y => y. Id). Contains(x.Id) || List2.

Single(y => x. Id == y.Id). HourBy.

I created my linq query in my project according to your example It was helped to me. Thanks – fyasar Oct 12 '10 at 8:14.

I think this is what you're after, based on the example lists you supplied... var list3 = new List(); foreach(var item in list1) { var totalFromOther = list2. Where(x => x. Id == item.Id).

Sum(y => y. HourBy); var newItem = new MyClass() { id = item.Id, HourBy = item. HourBy - totalFromOther }; if(newItem.

HourBy > 0) list3. Add(newItem); }.

Here's a solution: var qry = from ee in (from e1 in list1 join e2 in list2 on e1. Id equals e2. Id into gj from e in gj.DefaultIfEmpty() select new { e1.Id, MainHourBy = e1.

HourBy, SecondHourBy = (e == null? 0 : e. HourBy) }) where ee.

SecondHourBy el. SecondHourBy) }.

I use this code, it's not perfect but working and may be clearer for newbie in Linq (like me) List listRes = new List(); foreach ( MyClass item in list1) { var exist = list2 . Select(x => x. MyClass) .

Contains(item); if (!exist) listRes. Add(item); else { var totalFromOther = list2 . Where(x => x.MyClass.Id == item.

Id) . Sum(y => y. HourBy); if (item.

HourBy! = totalFromOther) { item. HourBy = item.

HourBy - totalFromOther; listRes. Add(item); } } } Thanks for you help.

Var agg1 = from l in list1 group l by l. ID into g select new { g. Key, Sum = g.

Sum(_ => _. HourBy) }; var agg2 = from l in list2 group l by l. ID into g select new { g.

Key, Sum = g. Sum(_ => _. HourBy) }; var q = from l1 in agg1 let l2 = agg2.

FirstOrDefault(_ => _. Key == l1. Key) let sum = l1.

Sum - (l2! = null? L2.

Sum : 0) where sum! = 0 select new MyClass(l1. Key, sum); var list3 = q.ToList().

I'm not overly familiar with the 'let' clause. Is it evaluated once for the query, or once per row in this case? – MattH Jul 28 '09 at 12:19.

Group list 2, so there is one entry per ID, with HourBy = total hour by var summedL2 = from item2 in List2 group item2 by item2. ID into g select new Test(g. Key, g.

Sum(item => item. HourBy)); var result = //Select items from list1 from item1 in List1 //Left join to our summed List2 join item2s in summedL2 on item1. ID equals item2s.ID into tempItem2s from item2 in tempItem2s.DefaultIfEmpty() //Where there is no match, or item2 is ID, item1.

HourBy - (item2 == null? 0 : item2. HourBy)).

The following code should do the trick: public List MyExtractionMethod(List List1, List List2) { var results = new List(); // add all the items in List1 that are not in List2 results. AddRange(List1. Where(x =>!

List2. Select(y => y. Id).

Contains(x. Id))); foreach (var item in List2) { var sum1 = List1. Where(c => c.Id == item.

Id). Sum(y => y. HoursBy); var sum2 = List2.

Where(c => c. Id == item.Id). Sum(y => y.

HoursBy); // check for uneven sums and only add the item if it hasn't already been added if (sum1! = sum2 &&!results. Select(x => x.

Id). Contains(item. Id)) { // do not overwrite the existing item in the list, create a new object results.

Add(new MyClass(item.Id, sum1 - sum2)); } } return results; }.

This is because when you use the Concat operator, the 2nd sequence (source2) will not be active until after the 1st sequence (source1) has finished pushing all its values. It is only after source1 has completed, then source2 will start to push values to the resultant sequence. The subscriber will then get all the values from the resultant sequence.

Compare this with the Merge operator. If you run the following sample code, you will get 1,1,2,2,3,3. This is because the two sequences are active at the same time and values are pushed out as they occur in the sources.

The resultant sequence only completes when the last source sequence has finished pushing values. Notice that for Merge to work, all the source observable sequences need to be of the same type of IObservable. The resultant sequence will be of the type IObservable.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions