执行您的示例说明的方法如下:
var minValue = someCollection.Min(x => x.start);
var result = from t in someCollection
where t.id > 5 // replace this line with your "where t == something"
where t.start == minValue
select t;
但是说您必须对集合中的每个元素与其他所有元素进行某种其他比较。有没有办法做这样的事情?
如果您确实需要将一个项目与列表中的所有其他项目进行比较,您可以像这样设计您的代码:
var result = from t in someCollection
where t.id > 5 // replace this line with your "where t == something"
let minValue = someCollection.Min (x => x.start)
where t.start == minValue
select t;
第二个示例的问题是,您访问的每个项目someCollection
都将被迫重新计算 minValue。
现在,这是一个完全人为的示例,说明在访问集合的每个成员时必须访问整个集合。它只是遍历项目列表并输出每个项目以及所有其他日期较短的项目:
var eventItems = new[]
{
new { Name = "alpha", DateCreated = DateTime.Today.AddDays(1) },
new { Name = "bravo", DateCreated = DateTime.Today.AddDays(2) },
new { Name = "charlie", DateCreated = DateTime.Today.AddDays(-1) },
new { Name = "delta", DateCreated = DateTime.Today.AddDays(-5) },
new { Name = "echo", DateCreated = DateTime.Today.AddDays(-3) },
new { Name = "foxtrot", DateCreated = DateTime.Today.AddDays(3) },
new { Name = "golf", DateCreated = DateTime.Today.AddDays(-4) }
};
var results = from item in eventItems
where item.Name.Length > 2
let prevDays = eventItems.Where (i => i.DateCreated < item.DateCreated)
select new
{
Name = item.Name,
CurrentDate = item.DateCreated,
PreviousItems = prevDays
};
输出:
也许这些示例之一将帮助您解决确切的问题。