在linq中加入两个查询的最佳方法是什么?目前我的代码没有将它们连接在一起。
var userprofilemodel1 =
(from u in db.UserProfiles
where u.UserId == 1
orderby u.FirstName, u.LastName
select new UserListViewModel
{
UserId = u.UserId,
UserName = u.UserName,
FirstName = u.FirstName,
LastName = u.LastName,
DocumentCount = u.Documents.Count
}).ToPagedList(page, 10);
var userprofilemodel2 =
(from u in db.UserProfiles
where u.UserId == 18
orderby u.FirstName, u.LastName
select new UserListViewModel
{
UserId = u.UserId,
UserName = u.UserName,
FirstName = u.FirstName,
LastName = u.LastName,
DocumentCount = u.Documents.Count
}).ToPagedList(page, 10);
userprofilemodel1.Concat(userprofilemodel2);
答案 0 :(得分:2)
这似乎应该这样做:
var userprofilemodel1 =
(from u in db.UserProfiles
where u.UserId == 1 || u.UserId == 18
orderby u.FirstName, u.LastName
select new UserListViewModel
{
UserId = u.UserId,
UserName = u.UserName,
FirstName = u.FirstName,
LastName = u.LastName,
DocumentCount = u.Documents.Count
}).ToPagedList(page, 10);
如果您希望/需要将其保留为两个单独的查询但是尝试连接结果,那么您需要意识到Concat
返回一个连锁列表 - 它不会< em>修改任一现有列表:
var mergedModel = userprofilemodel1.Concat(userprofilemodel2);