SQL“IN"的Linq版本陈述 [英] Linq version of SQL "IN" statement
问题描述
我有以下 3 个表作为简单项目标记"架构的一部分:
I have the following 3 tables as part of a simple "item tagging" schema:
==物品==
- ItemId int
- 品牌varchar
- 名称 varchar
- 花钱
- 条件变量
- 描述varchar
- 活动位
==标签==
- TagId int
- 名称 varchar
- 活动位
==TagMap==
- TagMapId int
- TagId int (fk)
- ItemId int (fk)
- 活动位
我想编写一个 LINQ 查询来返回匹配标签列表的项目(例如 TagId = 2,3,4,7).在我的应用程序上下文中,项目的示例将是计算机监视器"、连衣裙"、吉他"等,标签的示例将是电子产品"、服装"等.我通常会使用 SQL 完成此操作IN 语句.
I want to write a LINQ query to bring back Items that match a list of tags (e.g. TagId = 2,3,4,7). In my application context, examples of items would be "Computer Monitor", "Dress Shirt", "Guitar", etc. and examples of tags would be "electronics", "clothing", etc. I would normally accomplish this with a SQL IN Statement.
推荐答案
类似的东西
var TagIds = new int[] {12, 32, 42};
var q = from map in Context.TagMaps
where TagIds.Contains(map.TagId)
select map.Items;
应该做你需要的.这将生成一个 In ( 12, 32, 42 ) 子句(如果我没记错的话,更具体地说是一个参数化的 IN 子句).
should do what you need. This will generate an In ( 12, 32, 42 ) clause (or more specifically a parameterized IN clause if I'm not mistaken).
这篇关于SQL“IN"的Linq版本陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!