×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

In LINQ, keyword ‘in’ is used for other purposes. So you cannot directly use it in filter condition.

In order to use SQL IN condition in LINQ, you need break down to two steps:<br><br>

1. Create condition set <br>
2. Apply condition set in result query by <b>Queryable.Contains</b> or <b>Enumerable.Contains</b> method.<br><br>

For example,<br><br>

SQL query<br>

SELECT * FROM Products WHERE ProductID IN (SELECT ProductID FROM [Order_Details] WHERE OrderID = order_ID)<br><br>

By LINQ query<br><br>

NorthWindDB db = new NorthWindDB();<br>

// Condition set<br>
var itemQuery = from ProductItems in db.Order_Details<br>
where ProductItems.OrderID == order_ID<br>
select ProductItems.ProductID;<br><br>

// Result query<br><br>
var myProducts = from p in db.Products<br>
where itemQuery.Contains(p.ProductID)<br>
select p; <br>
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 这里有人用LINQ的吗?google了大半天一无所获,只能上来求教了。如何在用linq实现普通T-SQL的select * from Table1 where ID in s(s是字符串s1,s2,s3,...sn)?
    • 你会用google吗? 看看 asp.net
    • it should look like this: Table1.Where(x=>s.Contains(x.ID)).ToArray() or .ToList()
      • 谢大猫,我试试去。
        • 不谢不谢,我这几天也在钻研LINQ,深蓝的写法是传统LINQ,意思和我的一样,我用了Lamdar表达式。你们现在已经开始用.net 3.5开发项目了吗?
          • 嗯,经理坚持要用LINQ,我们只能follow,新东西总要用的,也不错,不过刚开始,所以很多东西要从新去research,进度比较慢。
            • 不错,佩服你们经理,我们也刚刚把项目全部转成.net 3.5的了。给我的感觉,LINQ就像程序员的毒品,你一旦沾上,就永远放不下了,成倍成倍的提高编程的效率。
          • Method approach + Lambda Expressions is more powerful than query approach. One advantage of the query approach is it looks closer to SQL query. And fortunately, sometimes, you can mix query with method like what I did.
    • In LINQ, keyword ‘in’ is used for other purposes. So you cannot directly use it in filter condition.
      In order to use SQL IN condition in LINQ, you need break down to two steps:<br><br>

      1. Create condition set <br>
      2. Apply condition set in result query by <b>Queryable.Contains</b> or <b>Enumerable.Contains</b> method.<br><br>

      For example,<br><br>

      SQL query<br>

      SELECT * FROM Products WHERE ProductID IN (SELECT ProductID FROM [Order_Details] WHERE OrderID = order_ID)<br><br>

      By LINQ query<br><br>

      NorthWindDB db = new NorthWindDB();<br>

      // Condition set<br>
      var itemQuery = from ProductItems in db.Order_Details<br>
      where ProductItems.OrderID == order_ID<br>
      select ProductItems.ProductID;<br><br>

      // Result query<br><br>
      var myProducts = from p in db.Products<br>
      where itemQuery.Contains(p.ProductID)<br>
      select p; <br>
      • 就等你回帖呢,谢谢深蓝:)。