Linq查询Where语句后不返回关系数据 [英] Linq Query after Where statement not returning relationship data

查看:67
本文介绍了Linq查询Where语句后不返回关系数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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} );

如果我正确理解您的意思,那就是您要寻找的.之后,您将获得带有匿名对象的集合,每个对象都具有StoresBllStore属性.

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屋!

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