当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How can I query on a match in a list of strings in Breeze?

问题描述:

I have this action in a WebAPI2, EF6 project.

[HttpGet]

public IQueryable<ProductListItem> ProductListItems()

{

// Db.Products is a DbSet<Product>

return Db.Products.ProductListItemProjection();

}

This is a ProductListItem It's not an actual entity - just a projection of one (Product):

public class ProductListItem : Product

{

public IEnumerable<string> TagNames { get; set; }

}

And here is the ProductListItemProjection extension method:

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...

});

}

Is it possible to construct a query in Breeze that queries all of the ProductListItems that have an element matching some given text in ProductTags? I'm not sure how to use the any operator with a list of strings, but I tried this:

var query = breeze.EntityQuery.from("ProductListItems")

.where("tagNames", "any", "" /* Not sure what to put here... just using empty string (also tried 'valueOf') */, "contains", "Expen");

But I get a JS error in the breeze library (during query construction, before execution):

Error: propertyOrExpr cannot be null except when using the 'IsTypeOf' operator

I can construct a LINQ to Entities query that works as expected:

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

But how can I do this in Breeze? Or is this unsupported? It appears to be supported in OData v3, but maybe Breeze doesn't support it yet...

Update

I might actually have a more general problem. I tried changing ProductListItem.TagNames to be a list of objects with a Name property, rather than just a list of strings. Then I tried:

var query = breeze.EntityQuery.from("ProductListItems")

.where("tagNames", "any", "name", "contains", "Expen");

And that gets past the query creation, but it fails in executeQuery before any request is made to the server :

TypeError {query: j, stack: (...), message: "Cannot read property 'isAnonymous' of undefined"}

分享给朋友:
您可能感兴趣的文章:
随机阅读: