Get data of all entities in a many-to-many relationship

帅比萌擦擦* 提交于 2019-12-02 03:20:53

The basic LINQ query shape for querying many-to-many relationships is:

from c in db.Customers
from f in c.Films // NOT db.Films
select new
{
    Customer = c.Customername,
    Film = f.FilmName,
    ...
}

This is the query syntax equivalent of SelectMany. The result is a flat list of data, hence this is commonly referred to as "flattening".

Often it's more useful to show a nested list:

from c in db.Customers
select new
{
    c.Customername,
    c. ...,
    Films = from f in c.Films
            select new
            {
                f.FilmName,
                f. ...,
            }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!