简体   繁体   中英

Linq — How to perform Join in ForEach statement?

The Linq Join examples I've seen illustrate hot ti Join when creating an anonymous type. How do I do the the Join in a ForEach statement.

eg

foreach (item i in MyContext.SomeEntity.Include("NavigationProperty1").Include("NavigationProperty2").Join(MyContext.SomeEntity2 on id == id)
{
}

Thanks!

Well, you're trying to mix query syntax with just calling the extension methods directly here - that's not going to work to start with.

But the result of a join is a sequence of pairs , effectively - pairs which have some property in common. It's not clear where "item" comes from - how do you want each pair from SomeEntity and SomeEntity2 to be transformed into an item ?

Your call is likely to end up looking something like:

...Join(MyContext.SomeEntity2, x => x.id, y => y.id, (x, y) => !!!)

where the !!! is the projection from a pair of entities to a single useful value.

See part 19 of my Edulinq blog series for more information about how the Join method works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM