-4

在 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);
4

1 回答 1

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 返回一个连接列表 - 它不会修改任何现有列表:

var mergedModel = userprofilemodel1.Concat(userprofilemodel2);
于 2013-09-03T02:00:15.820 回答