Reputation: 1945
I get array of strings from my client. Then in my webapi i split it as following
var splitId = string.Join(",", ids);
Then i my Linq query i use above variable
This is my Linq query
var query = tableARepository.AsQueryable()
.Where(a=> splitId.Contains(a.Id.ToString()))
.Select(a => new {
Id = a.Id
, Name = a.Name
, FkId = tableBRepository.AsQueryable().Min(b => b.fkid=a.id)
});
I get this exception:
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.","
This is because of Contains. How can i fix this error?
Upvotes: 1
Views: 2678
Reputation: 223412
Instead of converting your ids
to concatenated string, keep them in a List<T>/IEnumerable<T>
and then query against that like:
Based on your comment:
Ids are defined as string[] ids
You should convert them to IEnumerable<int>
or List<int>
like:
List<int> integerIds = ids.Select(int.Parse).ToList();
and then in your query:
var query = tableARepository.AsQueryable()
.Where(a=> integerIds.Contains(a.Id))
Just make sure that ids
contains elements of same type as your table Id
The reason you are getting the exception is due to the call to ToString
in your LINQ query, the provider is trying to convert it under laying data source language (May be SQL) but it can't.
Upvotes: 5