我有以下方法:
public static void UpdateKeySetToDatabase(List<TrackedKey> set)
{
try
{
using (var c = new SqlConnection(ConnectionString.ConnectionString))
{
c.Open();
//var result = c
//.Where(x => x.Product.Any(p => p.ProductNumber == "11"))
//.ToList();
DataContext dc = new DataContext(c);
Table<TrackedKey> tableKeys = dc.GetTable<TrackedKey>();
var KeyStates =
from k in tableKeys
select k;
foreach (var k in KeyStates)
{
EventHandling.Logging.CreateTextEvent($"Key last touch time {k}");
}
}
}
catch (Exception ex)
{
EventHandling.Logging.CreateTextEvent($"linq error {ex.ToString()}");
}
}
我想仅基于tableKeys中的键来生成keyStates数据,该键还存在于传入列表集中的属性“ keyId”中。怎么做?我试图在SO和其他页面上寻找答案,但是它们似乎显示了一些示例,其中您要过滤的列表仅包含一个属性,例如仅Keyid列表。我的列表是对象TrackedKey,其属性之一是KeyId。
答案 0 :(得分:3)
var KeyStates = from k in tableKeys
join s in set on k.keyId equals s.keyId
select s;
OR(可能效率较低)
var KeyStates = tableKeys.Where(x => set.Any(y => y.keyId == x.keyId));