SQL“IN”语句的Linq版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/896123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq version of SQL "IN" statement
提问by Brian David Berman
I have the following 3 tables as part of a simple "item tagging" schema:
我有以下 3 个表作为简单“项目标记”模式的一部分:
==Items==
==项目==
- ItemId int
- Brand varchar
- Name varchar
- Price money
- Condition varchar
- Description varchar
- Active bit
- 项目 ID 整数
- 品牌varchar
- 名称 varchar
- 价格金钱
- 条件变量
- 说明 varchar
- 活动位
==Tags==
==标签==
- TagId int
- Name varchar
- Active bit
- 标签 ID 整数
- 名称 varchar
- 活动位
==TagMap==
==标签映射==
- TagMapId int
- TagId int (fk)
- ItemId int (fk)
- Active bit
- TagMapId int
- TagId int (fk)
- ItemId int (fk)
- 活动位
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.
我想编写一个 LINQ 查询来带回与标签列表匹配的项目(例如 TagId = 2,3,4,7)。在我的应用程序上下文中,项目的示例将是“计算机监视器”、“连衣裙”、“吉他”等,标签的示例将是“电子产品”、“服装”等。我通常会使用 SQL 完成此操作IN 声明。
回答by Denis Troller
Something like
就像是
var TagIds = new int[] {12, 32, 42};
var q = from map in Context.TagMaps
where TagIds.Contains(map.TagId)
select map.Items;
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).
应该做你需要的。这将生成一个 In ( 12, 32, 42 ) 子句(或者如果我没记错的话,更具体地说是一个参数化的 IN 子句)。
回答by Luke Schafer
given array of items:
给定的项目数组:
var list = new int[] {2,3,4}
use:
用:
where list.Contains(tm.TagId)
回答by Amy B
List<int> tagIds = new List<int>() {2, 3, 4, 7};
int tagIdCount = tagIds.Count;
//
// Items that have any of the tags
// (any item may have any of the tags, not necessarily all of them
//
var ItemsAnyTags = db.Items
.Where(item => item.TagMaps
.Any(tm => tagIds.Contains(tm.TagId))
);
//
// Items that have ALL of the tags
// (any item may have extra tags that are not mentioned).
//
var ItemIdsForAllTags = db.TagMap
.Where(tm => tagIds.Contains(tm.TagId))
.GroupBy(tm => tm.ItemId)
.Where(g => g.Count() == tagIdCount)
.Select(g => g.Key);
//
var ItemsWithAllTags = db.Items
.Where(item => ItemsIdsForAllTags.Contains(item.ItemId));
//runs just one query against the database
List<Item> result = ItemsWithAllTags.ToList();
回答by Anish Karunakaran
You can simply use,
你可以简单地使用,
var TagIds = {12, 32, 42}
var prod =entities.TagMaps.Where(tagmaps=> TagIds .Contains(tagmaps.TagId));
回答by IR.Programmer
string[] names = {"John", "Cassandra", "Sarah"};
var results = (from n in db.Names
where names.Contains(n.Name)
select n).ToList();
回答by Micha
You may create an extension method "IN()"
您可以创建一个扩展方法“IN()”
public static class Extension
{
public static bool IN(this object anyObject, params object[] list)
{ return list.Contains(anyObject); }
}
to be used like this
像这样使用
var q = from map in Context.TagMaps
where map.TagId.IN(2, 3, 4, 7)
select map.Items;
Or just use the inline array.Contains() notation:
或者只是使用内联 array.Contains() 符号:
var q = from map in Context.TagMaps
where new[]{2, 3, 4, 7}.Contains(map.TagId)
select map.Items;