SQL"IN"的Linq版本陈述 [英] Linq version of SQL "IN" statement

查看:50
本文介绍了SQL"IN"的Linq版本陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为简单的项目标记"模式,我有以下3个表:

I have the following 3 tables as part of a simple "item tagging" schema:

==物品==

  • ItemId int
  • 品牌varchar
  • 名称varchar
  • 价格货币
  • 条件varchar
  • 描述varchar
  • 有效位

==标签==

  • TagId int
  • 名称varchar
  • 有效位

== TagMap ==

==TagMap==

  • TagMapId int
  • TagId int(fk)
  • ItemId int(fk)
  • 有效位

我想编写一个LINQ查询以带回与标签列表匹配的Item(例如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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆