C# 按嵌套列表在嵌套列表中进行可查询搜索

C# 按嵌套列表在嵌套列表中进行可查询搜索,c#,entity-framework-core,C#,Entity Framework Core,请有人能在下面帮助我吗 我必须去看类似的东西 Class Product { public string Name { get; set; } public virtual ICollection<ProductAttributes> ProductAttributes { get; set; } } class sc : Product { } class ProductAttributes { public long ProductID

请有人能在下面帮助我吗 我必须去看类似的东西

Class Product 
{
    public string Name { get; set; }       
    public virtual ICollection<ProductAttributes> ProductAttributes { get; set; }
}
class sc : Product { }

class ProductAttributes 
{
    public long ProductID { get; set; }
    public virtual Product Product { get; set; }
    public long AttributesDtlID { get; set; }
}
我也试过了

Product.Where(X => X.ProductAttributes.AsEnumerable().Any(m => sc.ProductAttributes.Any(A => m.AttributesDtlID == A.AttributesDtlID))); 
但是我得到了下面的错误

LINQ expression ''DbSet<Product>
.Where(p => p.IsDeleted == False)
.Where(p => (MaterializeCollectionNavigation(
    navigation: Navigation: Product.ProductAttributes,
    subquery: DbSet<ProductAttributes>
        .Where(p0 => EF.Property<Nullable<long>>(p, "ID") != null && EF.Property<Nullable<long>>(p, "ID") == EF.Property<Nullable<long>>(p0, "ProductID")))
    .AsEnumerable()
    .Any(m => __sc_ProductAttributes_0
        .Any(A => m.AttributesDtlID == A.AttributesDtlID)))'' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
LINQ表达式''DbSet
.其中(p=>p.IsDeleted==False)
.Where(p=>(MaterializeCollectionNavigation(
导航:导航:Product.ProductAttributes,
子查询:DbSet
其中(p0=>EF.Property(p,“ID”)!=null&&EF.Property(p,“ID”)==EF.Property(p0,“ProductID”))
.可计算的()
.Any(m=>\uuuuSC\uProductAttributes\u0
.Any(A=>m.AttributesDtlID==A.AttributesDtlID)))无法翻译“”。请以可以翻译的形式重写查询,或者通过插入对AsEnumerable()、AsAsAsAsAsyncEnumerable()、ToList()或ToListSync()的调用显式切换到客户端计算。请参阅https://go.microsoft.com/fwlink/?linkid=2101038 了解更多信息。

您的类没有ID字段,也不确定“sc”类的用途

但是,假设:

Class Product 
{
    public long ID { get; set; }      <- PK
    public string Name { get; set; }        
    public virtual ICollection<ProductAttributes> ProductAttributes { get; set; }
}

class ProductAttributes 
{
    public long ID { get; set; }    <- PK
    public long ProductID { get; set; }
    public virtual Product Product { get; set; }
    public long AttributesDtlID { get; set; }
}
然后使用:

List<int> attrDtlIdsFilter = new List<int> { 1, 2, 3 };
List<Product> products =
   Product.Where(p => 
       ProductAttributes.Where(pa => attrDtlIdsFilter.Contains(pa.AttributesDtlID))
                        .Select(pa => pa.ProductID)
                        .Contains(p.ID));

您的类没有ID字段,也不确定“sc”类的用途

但是,假设:

Class Product 
{
    public long ID { get; set; }      <- PK
    public string Name { get; set; }        
    public virtual ICollection<ProductAttributes> ProductAttributes { get; set; }
}

class ProductAttributes 
{
    public long ID { get; set; }    <- PK
    public long ProductID { get; set; }
    public virtual Product Product { get; set; }
    public long AttributesDtlID { get; set; }
}
然后使用:

List<int> attrDtlIdsFilter = new List<int> { 1, 2, 3 };
List<Product> products =
   Product.Where(p => 
       ProductAttributes.Where(pa => attrDtlIdsFilter.Contains(pa.AttributesDtlID))
                        .Select(pa => pa.ProductID)
                        .Contains(p.ID));
类产品
{
//主键
公共长Id{get;set;}
//产品名称
公共字符串名称{get;set;}
//与属性的关系
公共虚拟列表ProductAttributes{get;set;}
}
类ProductAttribute
{
//主键
公共长Id{get;set;}
//产品链接
公共长ProductID{get;set;}
公共虚拟产品产品{get;set;}
//这是您希望在查询中测试的属性(据我所知)
公共长属性DTLID{get;set;}
}
如果现在要获取所有具有属性id为“1230”的产品链接的产品,请执行以下操作:

DbSet<Product> Products; <-- this comes from the Entity Framework
long id = 1230;
List<Product> result;

result = Products
    .Where(p => p.ProductAttributes
        .Where(attr => attr.AttributesDtlID == id)
        .Count != 0)
    .ToList();
DbSet产品;p.ProductAttributes
.Where(attr=>attr.AttributesDtlID==id)
.Count!=0)
.ToList();
如果要对给定的ID列表执行相同的操作,请执行以下操作:

DbSet<Product> Products; <-- this comes from the Entity Framework
List<long> ids = new List<long>(){1230, 100, 20}
List<Product> result;

result = Products
    .Where(p => p.ProductAttributes
        .Where(attr =>  ids.Contains(attr.AttributesDtlID))
        .Count != 0)
    .ToList();
DbSet产品;p、 产品属性
.Where(attr=>ids.Contains(attr.AttributesDtlID))
.数一数!=0)
.ToList();
类别产品
{
//主键
公共长Id{get;set;}
//产品名称
公共字符串名称{get;set;}
//与属性的关系
公共虚拟列表ProductAttributes{get;set;}
}
类ProductAttribute
{
//主键
公共长Id{get;set;}
//产品链接
公共长ProductID{get;set;}
公共虚拟产品产品{get;set;}
//这是您希望在查询中测试的属性(据我所知)
公共长属性DTLID{get;set;}
}
如果现在要获取所有具有属性id为“1230”的产品链接的产品,请执行以下操作:

DbSet<Product> Products; <-- this comes from the Entity Framework
long id = 1230;
List<Product> result;

result = Products
    .Where(p => p.ProductAttributes
        .Where(attr => attr.AttributesDtlID == id)
        .Count != 0)
    .ToList();
DbSet产品;p、 产品属性
.Where(attr=>attr.AttributesDtlID==id)
.数一数!=0)
.ToList();
如果要对给定的ID列表执行相同的操作,请执行以下操作:

DbSet<Product> Products; <-- this comes from the Entity Framework
List<long> ids = new List<long>(){1230, 100, 20}
List<Product> result;

result = Products
    .Where(p => p.ProductAttributes
        .Where(attr =>  ids.Contains(attr.AttributesDtlID))
        .Count != 0)
    .ToList();
DbSet产品;p、 产品属性
.Where(attr=>ids.Contains(attr.AttributesDtlID))
.数一数!=0)
.ToList();

您能告诉我们您试图通过此查询完成什么吗?首先,由于您的错误,linq查询无法转换为数据库查询。@NathanielWalser我需要做的是让产品包含产品属性集合,并需要通过属性集合获取产品。我想模拟此SQL{select*from Product P join ProductAttributes A on P.ID=A.ID,其中A.AttributesDtlID位于(1,2,3)}您能告诉我们您试图用此查询完成什么吗?在第一次查看中,linq查询无法转换为数据库查询,因为您的错误说明了这一点。@NathanielWalser我需要做的是让产品包含产品属性集合,并需要通过属性集合获取产品。我想模拟此SQL{select*from Product P join ProductAttributes A on P.ID=A.ID其中A.AttributesDtlID in(1,2,3)}谢谢,这是一项工作,但是你能解释一下(.Any)为什么不起作用以及(.select(pa=>pa.ProductID).Contains(P.ID))的用途吗当我删除它时,我得到了一个错误,我更新了我的答案并提供了一个细分。Nathaniel的答案是解决同样问题的另一种方法。你应该对LINQ有一个很好的了解。谢谢,这项工作,但是你能解释一下为什么(.Any)不起作用,以及(.Select(pa=>pa.ProductID.Contains(p.ID))的用途吗当我删除它的时候,我得到了一个错误,我更新了一个细分的答案。纳撒尼尔的答案是另一种方法,可以达到同样的目的。你应该好好阅读LINQ。