0

我有 4 个实体,分别是零售商、商店、组、用户

每个零售商可以有许多商店和许多用户,用户只能是一个组的一部分,并且一个组中有许多商店,一个商店也可以在多个组中。出于这个原因,我有一个名为 storesingroups 的连接表。商店和用户有一个 RetailerId 作为外键。EF 模型中还有其他表链接,例如每个商店的用户详细信息和日志等。

我想要做的是返回链接到某个用户名的所有组,但只返回组详细信息和该组中的任何商店,链接到我不需要的商店实体的任何其他表。

我已经搞砸了许多不同的查询来尝试执行以下最接近的操作,但是我相信有更好的方法。该代码还导致内存不足异常,所以我知道某处不正确

 var results1 = (from g in db.Groups
                        from gs in db.StoresInGroups
                         from s in db.Stores
                     //    from r in db.Retailers
                       from u in db.UserProfileEFs
                       where s.RetailerRetailerId == u.RetailerId
                       where g.GroupId == gs.GroupId
                       where gs.StoreId == s.StoreId
                        where u.UserName == this.User.Identity.Name
                        where g.GroupId == u.GroupId
                        select new { Groups = g}).ToList();

编辑:

好的,因此设法通过此查询获得了我需要的结果:

var results2 = (from g in db.Groups
                           join u in db.UserProfileEFs on g equals u.Group where u.UserName == this.User.Identity.Name

                           select new { Group = g });

我现在需要做的是更改投影,因为它当前正在返回用户配置文件以及组中的商店,我如何让查询不返回用户配置文件?

4

0 回答 0