如何为了做某件事而不是为了选择某件事而加入 [英] How to join for the purpose of doing an action, but not for selecting anything

查看:44
本文介绍了如何为了做某件事而不是为了选择某件事而加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

listOne
    .Join(listTwo, 
          x => x.key, 
          y => y.key, 
          (x, y) => 
          { 
              y.SomeProperty = x.SomeProperty; 
              return 0; 
          });

我需要将一个属性从一个列表复制到另一个项目匹配的位置,并且我可以轻松地将两个列表合并,如上面的示例所示,但是我认为跟随我的另一个开发人员会认为这到底是什么?"因为我并没有真正按照预期的方式使用它. linq是否提供更合适的方法来完成同一件事?

I need to copy a property from one list to another where the items match, and I can easily join the two lists as shown in the example above but I think another developer coming after me would think "what the heck?" because I'm not really using it the way it was intended. Does linq provide a more appropriate way to accomplish the same thing?

或者,如果我希望代码可读性强,我应该坚持使用循环而不是尝试一行吗?

Or if I want my code to be readable, should I just stick with a loop instead of trying to one line it?

推荐答案

请注意,ParallelEnumerable<T>List<T>提供了对每个结果执行Action<T>的方法,但是这些是特定的可枚举类型,可能不适用于情况(旁注:通过 .AsParallel() )

Note that ParallelEnumerable<T> and List<T> provide methods to perform an Action<T> against each result, but these are specific enumerable types that may not be appropriate for the situation (side note: consider parallel for this particular situation via .AsParallel())

但是,默认实现是执行循环,而不是使用传递给JoinSelectFunc,因为正如您所说的那样,这可能会引起误解,并且需要未使用的返回值.像贾斯汀·皮洪尼(Justin Pihony)在回答中所说的那样,这违反了无副作用"的原则.

However, the default implementation is to perform a loop rather than using the Func passed to a Join or Select as, as you said, it can be misleading and necessitates an unused return value. To phrase this as Justin Pihony did in his answer, this violates principles of being "side effect free."

这是您应该考虑使用匿名类型的确切含义,

This is the exact siutuation in which you should consider using an anonymous type, like so

foreach(var pair in listOne
    .Join(listTwo, 
          x => x.key, 
          y => y.key, 
          (x, y) => new { first = x, second = y}))
{
    pair.first.SomeProperty = pair.second.SomeProperty;
}

较为冗长,但避免使用可能使某人不得不重复删除一行代码的方法.

More verbose, but avoids misusing methods in a way that might make someone have to do a double take down the line.

还请注意,尽管在Linq中还没有为您完成,但是您可以 编写自己的ForEach

Also note that while it's not done for you in Linq, you can write your own ForEach

public static void ForEach<T>(this IEnumerable<T> source, Action<T> act)
{
     foreach(T t in source) act(t);
}

使用相同的方法,建议偶尔为IEnumerables编写自己的扩展方法(我特别喜欢制作UpdateCollecitonTo来更新ObservableCollection中的项目,而不仅仅是替换引用...)这样,在参考源上浏览Join的源代码可能会有所帮助

Using this same approach, it's also advisable to occasionally write your own extension methods for IEnumerables (I'm particularly fond of making an UpdateCollecitonTo to update items in an ObservableCollection without just replacing the reference...) For this, it may be helpful to browse the source coe for Join on the Reference Source

这篇关于如何为了做某件事而不是为了选择某件事而加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆