I have IQueryable<someClass>
baseList
我有这个IQueryable < someClass > baseList
and List<someOtherClass>
someData
和< someOtherClass > someData列表
What I want to do is update attributes in some items in baseList.
我要做的是更新baseList中某些项目的属性。
For every item in someData, I want to find the corresponding item in baselist and update a property of the item.
对于someData中的每个项,我都希望找到baselist中的相应项,并更新该项的属性。
someOtherClass.someCode == baseList.myCode
someOtherClass。someCode = = baseList.myCode
can I do some type of join with Linq and set baseList.someData += someOtherClass.Da*tToConcantenate.
我可以用Linq和setbaselist来做一些类型的连接吗?someData + = someOtherClass.Da*tToConcantenate。
I could probably do this by iteration, but is there a fancy Linq way I can do this in just a couple lines of code?
我可能可以通过迭代来完成,但是有没有一种奇特的Linq方法可以在几行代码中完成这个任务呢?
Thanks for any tips, ~ck in San Diego
谢谢你给我的建议
5 个解决方案
#1
19
To pair elements in the two lists you can use a LINQ join:
要对两个列表中的元素进行配对,可以使用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:
这将为您提供与baseList中的对应项配对的someData中的每个项的枚举。从那里,你可以连接到一个循环:
foreach(var pair in pairs)
pair.b.SomeData += pair.d.Da*tToConcantenate;
If you really meant set concatenation rather than +=
, take a look at LINQ's Union, Intersect or Except methods.
如果您真正的意思是设置连接,而不是+=,请查看LINQ的Union、Intersect或Except方法。
#2
12
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.
LINQ用于查询——而不是更新。这意味着可以使用LINQ找到相应的项,但是对于修改,应该使用迭代。
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.
不可否认,您可能需要执行一些适当的查询,以便首先将baseList转换为有效的表单——例如,Dictionary
#3
3
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
:
您可以将IQueryable
List<SomeClass> convertedList = baseList.ToList();
convertedList.ForEach(sc =>
{
SomeOtherClass oc = someData.First(obj => obj.SomeCode == sc.MyCode);
if (oc != null)
{
sc.SomeData += oc.Da*tToConcatenate;
}
});
baseList = convertedList.AsQueryable(); // back to IQueryable
But it may be more efficient during this using non-LINQ constructs.
但是在使用非linq结构的过程中,它可能更有效。
#4
2
As mentioned before, it should be a combination of loop and LINQ
如前所述,它应该是循环和LINQ的组合
foreach (var someDataItem in someData)
{
someDataItem.PropertyToUpdate = (baseList.FirstOrDefault(baseListItem => baseListItem .key == someDataItem.key) ?? new SomeClass(){OtherProperty = "OptionalDefaultValue"}).OtherProperty;
}
#5
0
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:
你不能简单地找到一个列表中的对象而不是另一个列表中的对象,因为它们是两种不同的类型。我假设您正在比较一个名为OtherProperty的属性,该属性对于两个不同的类是通用的,并且具有相同的类型。在这种情况下,只使用Linq查询:
// 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);
#1
19
To pair elements in the two lists you can use a LINQ join:
要对两个列表中的元素进行配对,可以使用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:
这将为您提供与baseList中的对应项配对的someData中的每个项的枚举。从那里,你可以连接到一个循环:
foreach(var pair in pairs)
pair.b.SomeData += pair.d.Da*tToConcantenate;
If you really meant set concatenation rather than +=
, take a look at LINQ's Union, Intersect or Except methods.
如果您真正的意思是设置连接,而不是+=,请查看LINQ的Union、Intersect或Except方法。
#2
12
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.
LINQ用于查询——而不是更新。这意味着可以使用LINQ找到相应的项,但是对于修改,应该使用迭代。
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.
不可否认,您可能需要执行一些适当的查询,以便首先将baseList转换为有效的表单——例如,Dictionary
#3
3
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
:
您可以将IQueryable
List<SomeClass> convertedList = baseList.ToList();
convertedList.ForEach(sc =>
{
SomeOtherClass oc = someData.First(obj => obj.SomeCode == sc.MyCode);
if (oc != null)
{
sc.SomeData += oc.Da*tToConcatenate;
}
});
baseList = convertedList.AsQueryable(); // back to IQueryable
But it may be more efficient during this using non-LINQ constructs.
但是在使用非linq结构的过程中,它可能更有效。
#4
2
As mentioned before, it should be a combination of loop and LINQ
如前所述,它应该是循环和LINQ的组合
foreach (var someDataItem in someData)
{
someDataItem.PropertyToUpdate = (baseList.FirstOrDefault(baseListItem => baseListItem .key == someDataItem.key) ?? new SomeClass(){OtherProperty = "OptionalDefaultValue"}).OtherProperty;
}
#5
0
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:
你不能简单地找到一个列表中的对象而不是另一个列表中的对象,因为它们是两种不同的类型。我假设您正在比较一个名为OtherProperty的属性,该属性对于两个不同的类是通用的,并且具有相同的类型。在这种情况下,只使用Linq查询:
// 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);