我在WebAPI2,EF6项目中有这个动作。
[HttpGet]
public IQueryable<ProductListItem> ProductListItems()
{
// Db.Products is a DbSet<Product>
return Db.Products.ProductListItemProjection();
}
这是一个ProductListItem
它不是一个真实的实体 - 只是一个投影(Product
):
public class ProductListItem : Product
{
public IEnumerable<string> TagNames { get; set; }
}
这是ProductListItemProjection
扩展方法:
public static IQueryable<ProductListItem> ProductListItemProjection(this IQueryable<Product> products)
{
// Product.Tags is a list of ProductTags. A ProductTag has a TagType, which in the database is an int,
// but is backed by an enum (ProductTagTypes) in the .NET model. The purpose of this expression is to convert
// the Product.Tags list to a list of strings - display names depending on the TagType of the ProductTag.
Expression<Func<Product, IEnumerable<string>>> tagNames =
(Product p) => p.Tags
.Select(t =>
t.TagType == ProductTagTypes.Expensive ? "Expensive" :
t.TagType == ProductTagTypes.Red ? "Red" :
t.TagType == ProductTagTypes.Liquid ? "Liquid" : "");
return products
.AsExpandable() // From LINQKit...
.Select(p =>
new ProductListItem
{
// Set various properties inherited from Product
TagNames = tagNames.Invoke(p) // Using LINQKit...
});
}
是否可以在Breeze中构建一个查询,查询所有具有与ProductTags
中某些给定文本匹配的元素的ProductListItem?我不确定如何将any
运算符与字符串列表一起使用,但我尝试了这个:
var query = breeze.EntityQuery.from("ProductListItems")
.where("tagNames", "any", "" /* Not sure what to put here... just using empty string (also tried 'valueOf') */, "contains", "Expen");
但是我在breeze库中遇到了一个JS错误(在查询构造期间,执行之前):
Error: propertyOrExpr cannot be null except when using the 'IsTypeOf' operator
我可以构建一个按预期工作的LINQ to Entities查询:
// Returns ProductListItems that have a tag containing the string "Expen" (i.e., "Expensive")
Db.Users.Query().ProductListItemProjection()
.Where(u => u.TagNames.Any(r => r.Contains("Expen")));
但我怎样才能在微风中这样做?或者这是不受支持的?在OData v3中支持appears,但也许Breeze还不支持它......
更新
我可能实际上有一个更普遍的问题。我尝试将ProductListItem.TagNames
更改为具有Name
属性的对象列表,而不仅仅是字符串列表。然后我试了一下:
var query = breeze.EntityQuery.from("ProductListItems")
.where("tagNames", "any", "name", "contains", "Expen");
这超出了查询创建的范围,但在向服务器发出任何请求之前它在executeQuery
中失败:
TypeError {query: j, stack: (...), message: "Cannot read property 'isAnonymous' of undefined"}