How can I set properties on all items from a linq query with values from another object that is also pulled from a query?

断了今生、忘了曾经 提交于 2019-12-01 15:56:44

LINQ is designed for querying. If you're trying to set things, you should definitely use a loop (probably foreach). That doesn't mean you won't be able to use LINQ as part of that loop, but you shouldn't be trying to apply a side-effect within LINQ itself.

Query the OtherItems first. Do a ToDictionary() on the result. Then, when querying the database, do this:

var items = from i in context
   select new myClass 
   { A = i.A, 
     B = otherItems[i.A], 
     C = i.C
   } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!