使用Linq选择与列表匹配的表项

时间:2013-04-11 15:33:48

标签: entity-framework c#-4.0 linq-to-entities

我有一个'string,DeviceInfo'的字典,其中DeviceInfo是一个包含一些属性的类,如下所示:

    public string Name { get; set; }
    public string Id { get; set; }
    public bool Actioned { get; set; }

我需要使用Linq to Entities从表中的deviceId存在于字典的DeviceInfo“Name”属性中的表中选择设备详细信息。以下是我追求的东西。不幸的是我的代码不起作用。有谁告诉我我哪里出错了?

from t in _context.Devices where list.Select(x => x.Value.Name).Contains(t.DeviceId) select t

1 个答案:

答案 0 :(得分:1)

从词典中获取List<string>的简单Name

var names = list.Select(x => x.Value.Name).ToList();

然后在LINQ to Entities查询中使用它:

from t in _context.Devices
where names.Contains(t.DeviceId)
select t

如果您的SqlFunctions.StringConvertDeviceId

,则使用int
from t in _context.Devices
where names.Contains(SqlFunctions.StringConvert((double)t.DeviceId))
select t

它应该在SQL查询中生成IN语句。