C
C#2y ago
Zigo

❔ Linq statement doesn't work

.Where(i => strArray.Any(s => i.Name.Contains(s)))
.Where(i => strArray.Any(s => i.Name.Contains(s)))
I have an array of strings [ "text1", "text2", "text3"] and I want to find all Names that have a substring such as: text1somethingsomething
9 Replies
Zigo
Zigo2y ago
this is my entire code:
[HttpGet]
public async Task<IActionResult> GetContent([FromQuery] string[]? strArray)
{
// ** todo get this to order by OrderIndex **
List<DynamicContentDTO> dynContentList;

if (strArray == null)
dynContentList = await _context.DynamicContents
.OrderBy(x => x.OrderIndex)
.Select(d => new DynamicContentDTO(d))
.ToListAsync();
else
dynContentList = await _context.DynamicContents
.OrderBy(x => x.OrderIndex)
.Where(i => strArray.Any(s => i.Name.Contains(s)))
//.Where(i => i.Name.Contains(substring[0]))
.Select(d => new DynamicContentDTO(d))
.ToListAsync();

return Ok(dynContentList);
}
[HttpGet]
public async Task<IActionResult> GetContent([FromQuery] string[]? strArray)
{
// ** todo get this to order by OrderIndex **
List<DynamicContentDTO> dynContentList;

if (strArray == null)
dynContentList = await _context.DynamicContents
.OrderBy(x => x.OrderIndex)
.Select(d => new DynamicContentDTO(d))
.ToListAsync();
else
dynContentList = await _context.DynamicContents
.OrderBy(x => x.OrderIndex)
.Where(i => strArray.Any(s => i.Name.Contains(s)))
//.Where(i => i.Name.Contains(substring[0]))
.Select(d => new DynamicContentDTO(d))
.ToListAsync();

return Ok(dynContentList);
}
Saber
Saber2y ago
"doesn't work" isn't very helpful. whats the error
Zigo
Zigo2y ago
System.InvalidOperationException: The LINQ expression 's => EntityShaperExpression:
SalonML_API.Data.Models.DynamicContent
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.Name.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.
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitLambda[T](Expression`1 lambdaExpression)
System.InvalidOperationException: The LINQ expression 's => EntityShaperExpression:
SalonML_API.Data.Models.DynamicContent
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.Name.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.
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitLambda[T](Expression`1 lambdaExpression)
Saber
Saber2y ago
the strArray.Any portion probably isn't translatable
Zigo
Zigo2y ago
Stack Overflow
Using C# to check if string contains a string in string array
I want to use C# to check if a string value contains a word in a string array. For example, string stringToCheck = "text1text2text3"; string[] stringArray = { "text1", "someothertext", etc... }; ...
Saber
Saber2y ago
not everything you can do normal in LINQ is translatable to SQL with EF
Zigo
Zigo2y ago
ah..
Angius
Angius2y ago
You can use EF.Functions.Like() instead maybe
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.