Linq到对象更新 [英] Linq to objects Update
问题描述
我正在使用Linq-对象,我需要进行更新.我已经环顾了一段时间,但没有真正找到任何匹配的东西.
Im using Linq - objects and I need to do an update. I've been looked around for a while but not really found anything that matches.
因此,出于参数考虑,我有2个简单的List,两个都有一个键,因此我可以愉快地使用join.
So for arguments sake I have 2 simple List's, both with a key so I can use join happily.
我找不到将结果从obj2更新为obj1的简单方法.
I cant find a simple way to update results from obj2 into obj1.
对于简单的更新,我目前正在这样做:
for simple updates I'm currently doing this:
string newValue = "something";
var items = obj1.Where(w => w.ID == iKey).Select(c => {c.<property> = newValue; return c; }).ToArray();
一切正常,花花公子. 但是,如果我将第二个表添加到混合表中,并且想用obj2替换newValue.然后我遇到了问题.
And that all works well and dandy. However if I add into the mix my second table and I want to replace newValue with obj2. then I get problems.
主要是因为我随后切换到完整的linq语句,例如:
Primarily because I then switch to a full linq statement like:
var UPDATE = from o1 in obj1
join o2 in obj2 on o1.key equals o2.key
select ....;
它在这里被卡住了.
如果有人可以帮助我,我将非常感激! 预先感谢.
If anybody could help I would be extremly grateful! Thanks in advance.
推荐答案
您要在此处执行的操作与Linq并不是一件特别好的事情,因为Linq旨在查询数据而不是对其进行更新.您的Select
语句依赖于副作用来执行更新.通常应避免这种情况.
What you're trying to do here isn't a particularly good thing to do with Linq as Linq is meant to be for querying data and not updating it. Your Select
statement is relying on side-effects to perform the update. This generally should be avoided.
但是,您仍然可以做您想做的事.
However, you can still do what you want.
首先,我将您的查询重新排列为:
To start with, I re-arranged your query as:
var items =
obj1
.Where(w => w.Key == iKey)
.Select(c => { c.Value = newValue; return c; })
.ToArray();
然后我将其重构为:
Func<Obj, string, Obj> update =
(c, v) => { c.Value = v; return c; };
var items = (from w in obj1
where w.Key == iKey
select update(w, newValue)).ToArray();
这仍然有副作用,但是我使它更加明确(希望更具可读性).
This still has the side-effect, but I made it more explicit (and hopefully more readable).
鉴于此重构,涉及两个列表的UPDATE
查询变为:
Given this refactoring, the UPDATE
query involving the two lists becomes:
var UPDATE = (from o1 in obj1
join o2 in obj2 on o1.Key equals o2.Key
select update(o1, o2.Value)).ToArray();
如果您想这样做而没有副作用,我建议您采取以下措施:
If you wanted to do this without side-effects, I would suggest the following:
var items = from w in obj1
where w.Key == iKey
select (Action)(() => w.Value = newValue);
Array.ForEach(items.ToArray(), a => a());
var UPDATE = from o1 in obj1
join o2 in obj2 on o1.Key equals o2.Key
select (Action)(() => o1.Value = o2.Value);
Array.ForEach(UPDATE.ToArray(), a => a());
您可能不喜欢这种语法,因此您可以轻松地在IEnumerable<Action>
上编写快速扩展方法来调用操作,并使代码看起来像这样:
You might not like this syntax, so you could easily write quick extension method on IEnumerable<Action>
to invoke the actions and would make the code look like this:
(from w in obj1
where w.Key == iKey
select (Action)(() => w.Value = newValue)).Invoke();
(from o1 in obj1
join o2 in obj2 on o1.Key equals o2.Key
select (Action)(() => o1.Value = o2.Value)).Invoke();
我希望这会有所帮助.
这篇关于Linq到对象更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!