Compare lists and get values with Linq

12,586

Solution 1

I use this code, it's not perfect but working and may be clearer for newbie in Linq (like me)

List< MyClass> listRes = new List< MyClass>();
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,

Solution 2

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 < x.HourBy);

Best Regards

Solution 3

I think this is what you're after, based on the example lists you supplied...

var list3 = new List<MyClass>();

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

Solution 4

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 < ee.MainHourBy
                   group ee by ee.Id into g
                   select new MyClass
                   {
                       Id = g.Key,
                       HourBy = g.First().MainHourBy - g.Sum(el => el.SecondHourBy)
                   };
Share:
12,586
Kris-I
Author by

Kris-I

.NET Consulting

Updated on June 05, 2022

Comments

  • Kris-I
    Kris-I almost 2 years

    My object is "MyClass" with 2 properties : Id (int) and HourBy (int)

    I have two list :

    var List1 = new List<MyClass>();
    var List2 = new List<MyClass>();
    

    I'd like to get one list with : - Object from List1 present in List2 based on Id with Hourby (list2) < Hourby in List1 - Object from List1 no present in List2

    //#Sample1
    //List1 :
    List1.add(new MyClass(1,10));
    List1.add(new MyClass(2,20));
    List1.add(new MyClass(3,30));
    //List2 :
    List2.add(new MyClass(1,10)); 
    List2.add(new MyClass(2,15));
    //I'd like to get :
    new MyClass(2,5);
    new MyClass(3,30);
    
    //Sample2
    List1 :
    List1.add(new MyClass(1,10));
    List1.add(new MyClass(2,20));
    List1.add(new MyClass(3,30));
    //List2 :
    List2.add(new MyClass(1,10)); 
    List2.add(new MyClass(2,15));
    List2.add(new MyClass(2,2));
    //I'd like to get :
    new MyClass(2,3);
    new MyClass(3,30);
    

    Thanks,

    • James
      James almost 15 years
      Can you confirm this is what you want.....You want to return all items in list1 that are present in list2 but have a lower HourBy property value in List2, and all items that are present in List2 but not in List1?
    • Kris-I
      Kris-I almost 15 years
      @James : let's me try to rephrase. List1 as objects, List2 contains a copy of some object of List1. List2 can contain several time the same object. I'd like have a list of the object not present in List2 and if present (one or more) the some must be equal the value in list1 (HourBy) if not the object appear in the result list with the difference for HourBy between List1 and the List2 (the sum for the Id)
    • Kris-I
      Kris-I almost 15 years
      @MattH : agree, the samples are clearer :)
    • James
      James almost 15 years
      @Kris, ok I think I know what it is your trying to do. Let me update my answer!
  • fyasar
    fyasar over 13 years
    Hey Oliver, I created my linq query in my project according to your example It was helped to me. Thanks