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



请有人在Linq EF下面帮助我我有类似的对象

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=>具有SC的ProductAttributes=>产品属性我在下面创建了可查询

Product.Where(X => X.ProductAttributes.Any(m => sc.ProductAttributes.Any(A => m.AttributesDtlID == A.AttributesDtlID))); 

我也试过

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.
class Product 
{
// primary key
public long Id { get; set; }
// name of product
public string Name { get; set; }
// relation to attributes      
public virtual List<ProductAttribute> ProductAttributes { get; set; }
}
class ProductAttribute
{
// primary key
public long Id { get; set; }
// product link
public long ProductID { get; set; }
public virtual Product Product { get; set; }
// this is the property which you want to test in your query (as far as I have understood)
public long AttributesDtlID { 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();

如果你想对给定的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();

您的类没有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; }
}

并且您想要模拟(再次猜测联接应该是p.ID=A.ProductID(:

select * from Product P join ProductAttributes A on P.ID = A.ProductID where A.AttributesDtlID in (1,2,3)

然后使用:

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

更新

查询明细:

// Get a List of ProductAttributes where AttrobutesDtlID is in the List
// of attributesIds that we want to filter on
ProductAttributes.Where(pa => attrDtlIdsFilter.Contains(pa.AttributesDtlID))
// From that List, just 'select' ProductID from each ProductAttribute
.Select(pa => pa.ProductID)
// Looks to see if Product.ID is that List of ProductIDs
.Contains(p.ID)

相关内容

  • 没有找到相关文章

最新更新