C# 如何加入2个通用IEnumerator
我想知道是否有可能把IEnumerable合并在一起 基本上,我有很多用户,需要从数据库中获取他们的内容,以便我可以搜索和翻页 我正在使用LINQ to SQL,目前我的代码是:C# 如何加入2个通用IEnumerator,c#,asp.net,linq,C#,Asp.net,Linq,我想知道是否有可能把IEnumerable合并在一起 基本上,我有很多用户,需要从数据库中获取他们的内容,以便我可以搜索和翻页 我正在使用LINQ to SQL,目前我的代码是: public IEnumerable<content> allcontent; //Get users friends IEnumerable<relationship> friends = from f in db.relationships
public IEnumerable<content> allcontent;
//Get users friends
IEnumerable<relationship> friends = from f in db.relationships
where f.userId == int.Parse(userId)
select f;
IEnumerable<relationship> freindData = friends.ToList();
foreach (relationship r in freindData)
{
IEnumerable<content> content = from c in db.contents
where c.userId == r.userId
orderby c.contentDate descending
select c;
// This is where I need to merge everything together
}
公共IEnumerable所有内容;
//获得用户朋友
IEnumerable friends=来自数据库中的f
其中f.userId==int.Parse(userId)
选择f;
IEnumerable freindData=friends.ToList();
foreach(freindData中的关系r)
{
IEnumerable content=从c开始,在db.contents中
其中c.userId==r.userId
orderby c.contentDate降序
选择c;
//这是我需要将所有内容合并在一起的地方
}
我希望这有点道理
Matt如果要进行内部联接,请查看
.Intersect()
扩展方法。如果要进行内部联接,请查看.Intersect()
扩展方法。要合并哪些内容
您可以使用两个主要选项:.SelectMany(…)或.Concat(…)您要合并哪些内容
您可以使用两个主要选项:.SelectMany(…)或.Concat(…)如果我正确理解您正在尝试做什么,为什么不尝试做:
var result = from r in db.relationships
from c in db.contents
where r.userId == int.Parse(userId)
where c.userId == r.UserId
orderby c.contentDate descending
select new {
Relationship = r,
Content = c
}
这将为您提供一个
IEnumerable
,其中T
是一个匿名类型,具有字段关系和内容,如果我正确理解您正在尝试做什么,为什么不尝试做:
var result = from r in db.relationships
from c in db.contents
where r.userId == int.Parse(userId)
where c.userId == r.UserId
orderby c.contentDate descending
select new {
Relationship = r,
Content = c
}
这将为您提供一个IEnumerable
,其中T
是一个匿名类型,具有字段关系和内容,如果您知道您的用户将拥有少于2100个好友,您可以轻松地将已加载数据中的密钥发送回数据库:
List<int> friendIds = friendData
.Select(r => r.UserId)
.Distinct()
.ToList();
List<content> result = db.contents
.Where(c => friendIds.Contains(c.userId))
.ToList();
列出FriendID=friendData
.Select(r=>r.UserId)
.Distinct()
.ToList();
列表结果=db.contents
.Where(c=>friendIds.Contains(c.userId))
.ToList();
这里发生的事情是Linq将每个Id转换成一个参数,然后构建一个IN子句来进行过滤。2100是SQL server将接受的最大参数数。。。如果你有超过2100个朋友,你必须将ID列表拆分并合并(Concat)结果列表
或者,如果你想对你的问题有一个更直白的答案,Concat是一个方法,它通过创建一个新的IEnumerable将两个IEnumerable组合在一起,该IEnumerable返回第一个IEnumerable的项,然后返回第二个IEnumerable的项
IEnumerable<content> results = Enumerable.Empty<content>();
foreach (relationship r in friendData)
{
IEnumerable<content> content = GetData(r);
results = results.Concat(content);
}
IEnumerable results=Enumerable.Empty();
foreach(friendData中的关系r)
{
IEnumerable content=GetData(r);
结果=结果。浓度(含量);
}
如果您知道您的用户将拥有少于2100个好友,您可以轻松地将已加载数据中的密钥发送回数据库:
List<int> friendIds = friendData
.Select(r => r.UserId)
.Distinct()
.ToList();
List<content> result = db.contents
.Where(c => friendIds.Contains(c.userId))
.ToList();
列出FriendID=friendData
.Select(r=>r.UserId)
.Distinct()
.ToList();
列表结果=db.contents
.Where(c=>friendIds.Contains(c.userId))
.ToList();
这里发生的事情是Linq将每个Id转换成一个参数,然后构建一个IN子句来进行过滤。2100是SQL server将接受的最大参数数。。。如果你有超过2100个朋友,你必须将ID列表拆分并合并(Concat)结果列表
或者,如果你想对你的问题有一个更直白的答案,Concat是一个方法,它通过创建一个新的IEnumerable将两个IEnumerable组合在一起,该IEnumerable返回第一个IEnumerable的项,然后返回第二个IEnumerable的项
IEnumerable<content> results = Enumerable.Empty<content>();
foreach (relationship r in friendData)
{
IEnumerable<content> content = GetData(r);
results = results.Concat(content);
}
IEnumerable results=Enumerable.Empty();
foreach(friendData中的关系r)
{
IEnumerable content=GetData(r);
结果=结果。浓度(含量);
}
当前,它获取的是用户1的内容,然后是用户2,依此类推。我想把所有这些合并在一起。所以我最终得到了用户1+2+3等等。你的“这是哪里……”的评论让我很反感。(它在for循环中。)当前它获取user1的内容,然后是user2,依此类推。我想把所有这些合并在一起。所以我最终得到了用户1+2+3等等。你的“这是哪里……”的评论让我很反感。(在for循环中)非常感谢,伙计!我最终改变了一些事情,并编写了一个更好的LINQ语句,但这将非常方便!谢谢你,伙计!我最终改变了一些事情,并编写了一个更好的LINQ语句,但这将非常方便!