Linq to Object 引用列表
Linq to Object referencing a list
我有两个不同的数据源,一个有关于客户的详细信息,另一个只有一个 ClientID 的站点,但由于系统的部分迁移,我无法在数据库级别加入它们(然而,这最终会发生!):
var clients = _clientService.GetClientSummary(true);
var results = context.Sites.AsNoTracking().OrderByDescending(s => s.Id).
Skip((pageIndex - 1) * pageSize).Take(pageSize);
result.Content = pageResult.Select(a => new QuoteSearch
{
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
Customer = clients.Find(b => b.Id == a.ClientId).Name
}).ToList();
运行上面的代码returns一个错误
"LINQ to Entities does not recognize the method
'CertsAssured.Model.Client.ClientSummary
Find(System.Predicate`1[CertsAssured.Model.Client.ClientSummary])' "
我可以在此步骤之后编写代码来执行任务,但随后必须将 ClientId 保存到我的对象中以进行迭代。在 Select 方法期间是否有从客户端列表中获取信息的方法?
谢谢
设置数据库 filtering/paging 后,您可以使用 AsEnumerable
将 IQueryable 结果转换为内存中的 IEnumerable,您可以在其中对 clients
;[=14 进行查找=]
result.Content = pageResult
.AsEnumerable()
.Select(a => new QuoteSearch
{
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
Customer = clients.Find(b => b.Id == a.ClientId).Name
}).ToList();
如果数据库字段很多,不想全部从数据库中获取,可以先在IQueryable上过滤字段,比如;
result.Content = pageResult
.Select(a => new // This filters in the database
{ // to an anonymous type
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
ClientId = a.ClientId
})
.AsEnumerable() // Convert to an IEnumerable
.Select(a => new QuoteSearch // This is done in-memory
{ // generating the real type
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
Customer = clients.Find(b => b.Id == a.ClientId).Name
}).ToList();
我有两个不同的数据源,一个有关于客户的详细信息,另一个只有一个 ClientID 的站点,但由于系统的部分迁移,我无法在数据库级别加入它们(然而,这最终会发生!):
var clients = _clientService.GetClientSummary(true);
var results = context.Sites.AsNoTracking().OrderByDescending(s => s.Id).
Skip((pageIndex - 1) * pageSize).Take(pageSize);
result.Content = pageResult.Select(a => new QuoteSearch
{
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
Customer = clients.Find(b => b.Id == a.ClientId).Name
}).ToList();
运行上面的代码returns一个错误
"LINQ to Entities does not recognize the method 'CertsAssured.Model.Client.ClientSummary Find(System.Predicate`1[CertsAssured.Model.Client.ClientSummary])' "
我可以在此步骤之后编写代码来执行任务,但随后必须将 ClientId 保存到我的对象中以进行迭代。在 Select 方法期间是否有从客户端列表中获取信息的方法?
谢谢
设置数据库 filtering/paging 后,您可以使用 AsEnumerable
将 IQueryable 结果转换为内存中的 IEnumerable,您可以在其中对 clients
;[=14 进行查找=]
result.Content = pageResult
.AsEnumerable()
.Select(a => new QuoteSearch
{
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
Customer = clients.Find(b => b.Id == a.ClientId).Name
}).ToList();
如果数据库字段很多,不想全部从数据库中获取,可以先在IQueryable上过滤字段,比如;
result.Content = pageResult
.Select(a => new // This filters in the database
{ // to an anonymous type
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
ClientId = a.ClientId
})
.AsEnumerable() // Convert to an IEnumerable
.Select(a => new QuoteSearch // This is done in-memory
{ // generating the real type
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
Customer = clients.Find(b => b.Id == a.ClientId).Name
}).ToList();