var products =
(
from p in products
where !p.parentID.HasValue
select new ProductList
{
Name = p.Name,
Id = p.ID,
IsInStock = products.Any(...)
}
)
.ToArray();
我想调用ToDictionary()
并将Id作为键,将IsInStock作为值。
这可能吗?
答案 0 :(得分:4)
以下是使用可能有用的ToDictionary()
var x = new[]
{
new { Name = "Alice", Score = 50 },
new { Name = "Bob", Score = 40 },
new { Name = "Cathy", Score = 45 }
};
var y = x.ToDictionary(i => i.Name);
y的值是(来自LinqPad):
无论如何,我认为如果你添加ToDictionary(a => a.ID, b => b.IsInStock)
,你应该得到你想要的东西。
答案 1 :(得分:4)
试试这个:
var products =
(
from p in products
where !p.parentID.HasValue
select new ProductList
{
Name = p.Name,
Id = p.ID,
IsInStock = products.Any( ... )
}
)
.ToDictionary(y => y.ID, x => x.IsInStock);
答案 2 :(得分:1)
你有没有试过写它?
我认为很少看看InteliSense会告诉你ToDictinary有简单的覆盖,它会选择密钥和值的选择器:http://msdn.microsoft.com/en-us/library/bb548657.aspx