C# 用于检索项目的LINQ查询

C# 用于检索项目的LINQ查询,c#,asp.net,sql,linq,entity-framework,C#,Asp.net,Sql,Linq,Entity Framework,因此,我使用EF,我有以下实体: 网站 扇区 产品 属性 AttributeTag 这些关系如下: 我需要检索未直接链接到表的内容。例如需要扇区对象的产品,以便使用扇区.products之类的内容仅检索特定的产品 但是如果我需要检索给定网站下的所有产品,而不是它的父部门,该怎么办 在我的具体情况下,我的问题是: 1) 如何检索给定特定网站id的所有产品(不考虑行业) 2) 我如何检索具有特定标记\u id+网站\u id的所有产品。(同时检索其对应的属性) 谢谢你的帮助。谢谢 假设您具有两

因此,我使用EF,我有以下实体:

  • 网站
  • 扇区
  • 产品
  • 属性
  • AttributeTag
这些关系如下:

我需要检索未直接链接到表的内容。例如需要
扇区
对象的产品,以便使用
扇区.products
之类的内容仅检索特定的
产品

但是如果我需要检索给定
网站下的所有
产品
,而不是它的父
部门
,该怎么办

在我的具体情况下,我的问题是: 1) 如何检索给定特定
网站id的所有产品(不考虑行业)
2) 我如何检索具有特定
标记\u id
+
网站\u id
的所有产品。(同时检索其对应的
属性


谢谢你的帮助。谢谢

假设您具有两侧导航属性:

您将在产品实体中有一个
列表扇区列表

您将在扇区实体中有一个
列表ProductList

sectors\u products
不会作为一个实体出现,因为在对象世界中不需要它)

您将在部门实体中拥有一个
网站

您将在产品实体中有一个
列表属性英语

products\u标签
不会作为实体出现,因为在对象世界中不需要它)

1) 比如:

var result = ProductEntities
             .Where(p => p.SectorList
                         .Any(s => s.WebSite.Id == <your_website_id>)
                    );
var result=ProductEntities
.Where(p=>p.SectorList
.Any(s=>s.WebSite.Id==)
);
2) 类似于(以1作为基本查询)

result=result
.其中(p=>p.attribute英语
.Any(at=>at.Id==)
);
还是一个整体

var result = ProductEntitites
              .Where(p => 
                        p.SectorList.Any(s => s.WebSite.Id == <your_website_id>) &&
                        p.AttributeTagList.Any(at => at.Id == <your_tag_id>)
                     );
var result=productentites
.其中(p=>
p、 SectorList.Any(s=>s.WebSite.Id==)&&
p、 AttributeTagList.Any(at=>at.Id==)
);

模式中的关系形成一条路径。如果你想弄清楚两个实体集之间的关系,你必须遵循这条路径并查询中间的所有实体

var part1 = (from w in Websites
             from s in Sectors
             from p in s.Products
             where s.Website equals w
             && w.website_id equals web_id
             select p).Distinct();

var part2 = from p in part1
            let attr = p.Attributes.Where(a => a.tag_id + web_id == target_val)
            where attr.Any()
            select new { p, attr };
如果我正确地理解了您的模式,那么应该可以下拉数据来回答问题的两个部分

var part1 = (from w in Websites
             from s in Sectors
             from p in s.Products
             where s.Website equals w
             && w.website_id equals web_id
             select p).Distinct();

var part2 = from p in part1
            let attr = p.Attributes.Where(a => a.tag_id + web_id == target_val)
            where attr.Any()
            select new { p, attr };