Reputation: 4723
I have a dictionary of 'string, DeviceInfo' where DeviceInfo is a class containing a few properties as shown below:
public string Name { get; set; }
public string Id { get; set; }
public bool Actioned { get; set; }
I need to use Linq to Entities to select device details from a table where the deviceId in the table exists in the DeviceInfo “Name” property in the dictionary. Something like the following is what I’m after. Unfortunately my code doesn’t work. Anyone show me where I’m going wrong?
from t in _context.Devices where list.Select(x => x.Value.Name).Contains(t.DeviceId) select t
Upvotes: 0
Views: 2484
Reputation: 125650
Get simple List<string>
with Name
s from dictionary:
var names = list.Select(x => x.Value.Name).ToList();
And then use it within your LINQ to Entities query:
from t in _context.Devices
where names.Contains(t.DeviceId)
select t
Or with SqlFunctions.StringConvert
if your DeviceId
is and int
:
from t in _context.Devices
where names.Contains(SqlFunctions.StringConvert((double)t.DeviceId))
select t
It should generate IN
statement into SQL query.
Upvotes: 1