将 Llinq 查询从所有匹配转换为 OR(至少需要匹配一个标签)
Convert a Llinq query from All matches to OR (at least one tag needs to match)
此查询 return 的门 ID,door_id 的所有标签都匹配。
List<decimal> matchingDoors db.tags
.Where(x => x.user_id == userId && (null == SystemId|| x.syid == SystemId))
.GroupBy(x => x.door_id)
.Where(x => tags.All(y =>
x.Any(z => z.name == y)))
.Select(x => x.Key).ToList<decimal>();
如何将查询更改为 return 至少匹配一个标签 ('OR') 的结果?
您应该在 tags
集合上使用 Contains()
方法。你在这里:
List<decimal> matchingDoors db.tags
.Where(x => x.user_id == userId && (null == SystemId|| x.syid == SystemId))
.GroupBy(x => x.door_id)
.Where(x => x.Any(y => tags.Contains(y))) //this is where magic
.Select(x => x.Key).ToList<decimal>();
此查询 return 的门 ID,door_id 的所有标签都匹配。
List<decimal> matchingDoors db.tags
.Where(x => x.user_id == userId && (null == SystemId|| x.syid == SystemId))
.GroupBy(x => x.door_id)
.Where(x => tags.All(y =>
x.Any(z => z.name == y)))
.Select(x => x.Key).ToList<decimal>();
如何将查询更改为 return 至少匹配一个标签 ('OR') 的结果?
您应该在 tags
集合上使用 Contains()
方法。你在这里:
List<decimal> matchingDoors db.tags
.Where(x => x.user_id == userId && (null == SystemId|| x.syid == SystemId))
.GroupBy(x => x.door_id)
.Where(x => x.Any(y => tags.Contains(y))) //this is where magic
.Select(x => x.Key).ToList<decimal>();