Linq查询Where语句后不返回关系数据 [英] Linq Query after Where statement not returning relationship data
问题描述
public List<Store> GetAllFriendsTopStores(long uid, List<long> users)
{
using (var entities = new myEntities()) {
var v = entities.DbUsers
.Where( x => users.Contains( x.uid ))
.Stores // i can't select stores here? Why is this???
}
}
在这种情况下,商店"是一个导航属性...我需要能够在此场景中为我的朋友访问商店对象.
In this case Stores is a navigation property... I need to be able to access the stores objects for my friends in this scenerio.
推荐答案
在Where
之后,您拥有IEnumerable
集合,因此,如果您只有一个用户作为结果并想要访问它,请应用Single()
>
After Where
you have IEnumerable
collection, so if you have only one user as the result and want to access it - apply Single()
var v = entities.DbUsers
.Where( x => users.Contains( x.uid ))
.Single()
.Stores;
UPD :
var v = entities.DbUsers
.Where( x => users.Contains( x.uid ))
.Select( x => x.Stores );
在这种情况下,您将获得用户Stores
的集合.
In this case you will get the collection of users' Stores
.
UPD 2 :
var v = entities.DbUsers
.Where( x => users.Contains( x.uid ))
.Select( x => new { Stores = x.Stores, BllStore = new BllStore{a=x.a} );
如果我正确理解您的意思,那就是您要寻找的.之后,您将获得带有匿名对象的集合,每个对象都具有Stores
和BllStore
属性.
If I understand you correctly - that is what you are looking for. After this you have the collection with anonymous objects, each of which has Stores
and BllStore
property.
这篇关于Linq查询Where语句后不返回关系数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!