var searchs = new List<string> { "a1", "a2", "a3" };
var result = db.Products.Where(p => searchs.Any(s => p.Content.Contains(s))).ToList();
(Content is a string) This query work fine in asp.net mvc 5 but when i update to vs 2022 with asp.net mvc core .net 6, this query erorr like that
System.InvalidOperationException: 'The LINQ expression 's => EntityShaperExpression:
Website.Models.Product
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.Content.Contains(s)' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
i don't know why, anyone help me, thanks! sorry my English not good!
I have searched many solutions but still can't find it, I want help
update: (17 july 2023)
i see example like my query at this link (Ef 8.0): https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/whatsnew
var beers = new[] { "Carling", "Heineken", "Stella Artois", "Carlsberg" };
var pubsWithLager = await context.Pubs
.Where(e => beers.Any(b => e.Beers.Contains(b)))
.Select(e => e.Name)
.ToListAsync();
update 01 Dec 2023 i use .net 8 and it doesn't error anymore
You can use LINQKit's PredicateBuilder:
var predicate = searchs.Aggregate(PredicateBuilder.New<Products>(),
(e, s) => e.Or(p => p.Content.Contains(s))); // build the predicate
var result = db.Products
.Where(predicate)
.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With