linq 两个集合的交集的判断条件

xuexiaodong2009
博客专家认证
2011-06-16 12:58:58
例如y.Regions是一个集合,regionList也是一个集合,

把这两个集合有交集的记录查询出来,linq查询条件怎么写

(from y in db.Investors where (y.Regions))
...全文
789 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuexiaodong2009 2011-06-16
  • 打赏
  • 举报
回复
谢谢了,犯了一个低级错误




q107770540 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 xuexiaodong2009 的回复:]
db.Investors 包含一个字段Regions, Regions是一个int的集合,
regionList也是一个int的集合,
[/Quote]
你确定你数据中这两个都是int的集合么?
单步调试吧
xuexiaodong2009 2011-06-16
  • 打赏
  • 举报
回复
就编译不过去,划红线了
Error 15 Cannot convert lambda expression to type 'string' because it is not a delegate type D:\newwork\SourceCode\Equidity\Equidity.Web\Repositories\InvetorRepository..cs 114 3 Equidity.Web
Error 16 Delegate 'System.Func<Equidity.Web.Repositories.DataEntities.Investor,int,bool>' does not take 1 arguments D:\newwork\SourceCode\Equidity\Equidity.Web\Repositories\InvetorRepository..cs 114 3 Equidity.Web
Error 17 The best overloaded method match for 'System.Collections.Generic.List<int>.Contains(int)' has some invalid arguments D:\newwork\SourceCode\Equidity\Equidity.Web\Repositories\InvetorRepository..cs 114 26 Equidity.Web
Error 18 Argument 1: cannot convert from 'Equidity.Web.Repositories.DataEntities.Region' to 'int' D:\newwork\SourceCode\Equidity\Equidity.Web\Repositories\InvetorRepository..cs 114 46 Equidity.Web
q107770540 2011-06-16
  • 打赏
  • 举报
回复
专业点 有什么错误?
xuexiaodong2009 2011-06-16
  • 打赏
  • 举报
回复
var query= from y in db.Investors
where y.Regions.Any(x=>regionList.Contains(x))
select y

这样混用,有错误的
q107770540 2011-06-16
  • 打赏
  • 举报
回复
var query= from y in db.Investors
where y.Regions.Any(x=>regionList.Contains(x))
select y;

试试 这个
xuexiaodong2009 2011-06-16
  • 打赏
  • 举报
回复
var query= from y in db.Investors
from x in y.Regions
where regionList.Contains(x)
select y;

这个有错误的
xuexiaodong2009 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 q107770540 的回复:]

var query= from y in db.Investors
from x in y.Regions
where regionList.Contains(x)
select y;

你能把 db.Investors的结构简单帖一下么
还有regionList
[/Quote]
db.Investors 包含一个字段Regions, Regions是一个int的集合,
regionList也是一个int的集合,
在这两个集合有交集时查询记录,但要返回db.Investors的记录,不是单纯的一个字段Regions
xuexiaodong2009 2011-06-16
  • 打赏
  • 举报
回复
(from y in db.Investors where (y.Regions) select y)

条件不会写
我需要返回y,条件是集合有交集
q107770540 2011-06-16
  • 打赏
  • 举报
回复
var query= from y in db.Investors
from x in y.Regions
where regionList.Contains(x)
select y;

你能把 db.Investors的结构简单帖一下么
还有regionList
xuexiaodong2009 2011-06-16
  • 打赏
  • 举报
回复

(from y in db.Investors where (y.Regions) select y)

条件不会写
我需要返回y,不是 条件是集合有交集
程先森 2011-06-16
  • 打赏
  • 举报
回复
特别喜欢督察的答案。

简洁利落,准确,附有运行结果。

虽然我在格式上加以模仿,但是在程序质量上还是虽不能至,只能心向往之了。.....
xuexiaodong2009 2011-06-16
  • 打赏
  • 举报
回复
Intersect没有对应的关键字吗?
想from 就对应 from
q107770540 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 xuexiaodong2009 的回复:]
例如y.Regions是一个集合,regionList也是一个集合,

把这两个集合有交集的记录查询出来,linq查询条件怎么写

(from y in db.Investors where (y.Regions))
[/Quote]
var query= from y in db.Investors
from x in y.Regions
where regionList.Contains(x)
select x;

具体的需求楼主可详细说明一下 或举个现成的例子
threenewbee 2011-06-16
  • 打赏
  • 举报
回复
特别喜欢督察的答案。

简洁利落,准确,附有运行结果。

虽然我在格式上加以模仿,但是在程序质量上还是虽不能至,只能心向往之了。
q107770540 2011-06-16
  • 打赏
  • 举报
回复
q107770540 2011-06-16
  • 打赏
  • 举报
回复
 int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };

var commonNumbers = numbersA.Intersect(numbersB);

Console.WriteLine("Common numbers shared by both arrays:");
foreach (var n in commonNumbers)
{
Console.WriteLine(n);
}


Result

Common numbers shared by both arrays:
5
8

q107770540 2011-06-16
  • 打赏
  • 举报
回复
var result=y.Regions.Intersect(regionList);

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧