Linq to update a collection with values from another collection?

前端 未结 5 1163
北恋
北恋 2020-12-31 04:05

I have IQueryable baseList

and List someData

What I want to do is update attributes in some

相关标签:
5条回答
  • 2020-12-31 04:23

    You can convert the IQueryable<SomeClass> into a List<SomeClass>, use the ForEach method to loop over it and update the elements, then convert back to IQueryable:

    List<SomeClass> convertedList = baseList.ToList();
    convertedList.ForEach(sc =>
    {
        SomeOtherClass oc = someData.First(obj => obj.SomeCode == sc.MyCode);
        if (oc != null)
        {
            sc.SomeData += oc.DataIWantToConcatenate;
        }
    });
    
    baseList = convertedList.AsQueryable(); // back to IQueryable
    

    But it may be more efficient during this using non-LINQ constructs.

    0 讨论(0)
  • 2020-12-31 04:24

    As mentioned before, it should be a combination of loop and LINQ

    foreach (var someDataItem in someData)
    {
        someDataItem.PropertyToUpdate = (baseList.FirstOrDefault(baseListItem => baseListItem .key == someDataItem.key) ?? new SomeClass(){OtherProperty = "OptionalDefaultValue"}).OtherProperty;
    }
    
    0 讨论(0)
  • 2020-12-31 04:28

    You can't simply find objects that are in one list but not the other, because they are two different types. I'll assume you're comparing a property called OtherProperty that is common to the two different classes, and shares the same type. In that case, using nothing but Linq queries:

    // update those items that match by creating a new item with an
    // updated property
    var updated =
        from d in data
        join b in baseList on d.OtherProperty equals b.OtherProperty
        select new MyType()
        {
            PropertyToUpdate = d.PropertyToUpdate,
            OtherProperty = d.OtherProperty
        };
    
    // and now add to that all the items in baseList that weren't found in data
    var result =
        (from b in baseList
         where !updated.Select(x => x.OtherProperty).Contains(b.OtherProperty)
         select b).Concat(updated);
    
    0 讨论(0)
  • 2020-12-31 04:29

    To pair elements in the two lists you can use a LINQ join:

    var pairs = from d in someData
                join b in baseList.AsEnumerable()
                    on d.someCode equals b.myCode
                select new { b, d };
    

    This will give you an enumeration of each item in someData paired with its counterpart in baseList. From there, you can concatenate in a loop:

    foreach(var pair in pairs)
        pair.b.SomeData += pair.d.DataIWantToConcantenate;
    

    If you really meant set concatenation rather than +=, take a look at LINQ's Union, Intersect or Except methods.

    0 讨论(0)
  • 2020-12-31 04:31

    LINQ is for querying - not for updating. That means it'll be fine to use LINQ to find the corresponding item, but for the modification you should be using iteration.

    Admittedly you might want to perform some appropriate query to get baseList into an efficient form first - e.g. a Dictionary<string, SomeClass> based on the property you'll be using to find the corresponding item.

    0 讨论(0)
提交回复
热议问题