我在表Installation
上执行查询,以获取在不同数据库上查询表Product
所需的ID列表(在您提出之前,必须这样,可以不跨dbs查询。这是列表的样子:
class Installation
{
Guid InstallationId
Guid ProductId
}
List<Installation> installs;
我在使用此列表查询Product
表时没有任何问题,如下所示:
var prods = (from p in context.Product
where installs.Select(i => i.ProductId).Contains(p.ProductId)
select new
{
ProductNumber = p.ProductNumber
// How do I get InstallationId from installs??
}).ToList();
我需要知道的是如何从列表中检索InstallationId并将其存储在新列表中?
提前致谢。
答案 0 :(得分:3)
你应该加入
var products = (from product in context.Product
join install in installs
on install.ProductId equals product.ProductId
select new {
ProductNumber = product.ProductNumber
InstallationId = install.InstallationId
}
).ToList();
答案 1 :(得分:0)
对Jason的答案进行轻微重构以允许LinqToSql异常。
var products = (from product in context.Product
where installs.Select(i => i.ProductId).Contains(p.ProductId)
select product).ToList()
var installedProducts = (from product in products
join install in installs
on install.ProductId equals product.ProductId
select new
{
ProductNumber = product.ProductNumber,
InstallationId = install.InstallationId
}).ToList();