您好我正在寻找有关如何将行附加到现有LINQ对象的帮助。在下面的控制器方法中,我有两个结果集,我正在循环站点,并希望为站点对象中的每个记录的“结果”对象添加记录。
我已尝试过concat等但没有到达任何地方,只需要一个小例子来协助,非常感谢提前,J
public IQueryable<UsersToSite> FindAllUsersToSites(int userId,SystemType obj)
{
var results = (from usersToSite in this._db.UsersToSites
where usersToSite.UserId == userId &&
usersToSite.SystemTypeId == obj
orderby usersToSite.Site.SiteDescription
select usersToSite);
// Now for each remaining Site append a record thats not physically in the database. From the view the user will be able to click these records to ADD new
// I'll then build in a search
var sites = (from site in this._db.Sites
where !(from o in _db.UsersToSites where (o.UserId == userId && o.SystemTypeId == obj) select o.SiteId).Contains(site.SiteId)
orderby site.SiteDescription
select site);
foreach (var site in sites)
{
// HERE I want to create the new ROW in results object
//results = new[results] { new { UsersToSiteId = null, AccessTypeId = null } }.Concat(sites);
//SiteId=site.SiteId,
//UsersToSiteId = 0,
//AccessTypeId = 0,
//UserId = userId
}
return results;
}
答案 0 :(得分:0)
我认为你不能,如果你想保持可查询。 但是,如果使用ToList()实现结果,则可以:
var sites = (from site in this._db.Sites
where !(from o in _db.UsersToSites where (o.UserId == userId && o.SystemTypeId == obj) select o.SiteId).Contains(site.SiteId)
orderby site.SiteDescription
select site)
.ToList();
sites.Add(new Site { UsersToSiteId = null, etc });
如果它是LINQ to Objects,你可以Concat
这里的问题是它无法执行Concat
LINQ查询,该查询将包含来自SQL的一部分和来自对象的另一部分。您需要先实现结果,然后连接到对象。