如何使用LINQ写ContainsAll查询? (C#LINQ到SQL) [英] How to write a ContainsAll query using LINQ? (C# LINQ-TO-SQL)

查看:201
本文介绍了如何使用LINQ写ContainsAll查询? (C#LINQ到SQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下

var mustHave = new int [] { 1, 2 }

和名为db.Numbers的表。

and a table named db.Numbers

public class Number
{
     public int id;
     public int number;
}

填充

Numbers.add(new Number(){id=8,number=1});
Numbers.add(new Number(){id=8,number=2});
Numbers.add(new Number(){id=8,number=3});
Numbers.add(new Number(){id=9,number=1});
Numbers.add(new Number(){id=9,number=3});

我想获取所有与所有 mustHave变量。例如查询必须返回id = 8,但不返回id = 9。

I want to get all ids which are associated to all numbers in the mustHave variable. For example The query must return id=8 but not id=9. The real case is much more complicated and does the query against a database with Linq-To-Sql.

推荐答案

很好,如果你'在LINQtoSQL中使用DataContext做一些事情,尝试这样:

Well if you're doing something in LINQtoSQL with a DataContext, try this:

var mustHave = new int [] { 1, 2 };
var items = 
  from x in DataContext.SomeTable
  where mustHave.Contains(x.SomeProperty)
  select x;

这篇关于如何使用LINQ写ContainsAll查询? (C#LINQ到SQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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