C# 如何在带有条件的一列中使用LINQ select
像这样的数据表C# 如何在带有条件的一列中使用LINQ select,c#,linq,select,C#,Linq,Select,像这样的数据表 Date Count 20160101 100 20160102 103 20160103 108 20160104 102 20160105 104 20160106 106 20160107 108 我想要选择if=>soday.Count>soday[-3]。Count 结果=下面的3行: 20160104,因为102>100 20160105,因为104>103 20160107,因为108>102 请告诉我如何使用LINQ? 非
Date Count
20160101 100
20160102 103
20160103 108
20160104 102
20160105 104
20160106 106
20160107 108
我想要选择if=>soday.Count>soday[-3]。Count
结果=下面的3行:
20160104,因为102>10020160105,因为104>103
20160107,因为108>102 请告诉我如何使用LINQ?
非常感谢您可以使用重载,它将
Func谓词
作为输入。此委托的第二个输入是当前元素的索引。所以,这意味着lambda表达式必须接受两个输入,第一个是元素的类型,另一个是Int32
。
其中
方法将自动计算当前元素的索引
var result = myColl.Where((x, index) => index >= 3 && x.Count > myColl.ElementAt(index - 3).Count);
然后您可以使用所需的方法,如Select()
,ToList()
等
p.S:我假设对象的名称是myColl
另外:
我总是喜欢告诉开发者关于http://referencesource.microsoft.com/
。您可以很容易地找到所有方法的实现以及关于C#源代码的所有内容。
如果您感兴趣,这里是这个重载Where
方法的源代码
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate) {
if (source == null) throw Error.ArgumentNull("source");
if (predicate == null) throw Error.ArgumentNull("predicate");
return WhereIterator<TSource>(source, predicate);
}
您可以使用重载,它将Func谓词
作为输入。此委托的第二个输入是当前元素的索引。所以,这意味着lambda表达式必须接受两个输入,第一个是元素的类型,另一个是Int32
。
其中
方法将自动计算当前元素的索引
var result = myColl.Where((x, index) => index >= 3 && x.Count > myColl.ElementAt(index - 3).Count);
然后您可以使用所需的方法,如Select()
,ToList()
等
p.S:我假设对象的名称是myColl
另外:
我总是喜欢告诉开发者关于http://referencesource.microsoft.com/
。您可以很容易地找到所有方法的实现以及关于C#源代码的所有内容。
如果您感兴趣,这里是这个重载Where
方法的源代码
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate) {
if (source == null) throw Error.ArgumentNull("source");
if (predicate == null) throw Error.ArgumentNull("predicate");
return WhereIterator<TSource>(source, predicate);
}
一种方法是这样做
int index = 0;
var a = from i in someday
let indexNow = index++
where indexNow >= 3
let j = someday[indexNow - 3]
where i.Count > j.Count
select i;
创建临时变量j以在三步之前获取元素,然后将其与当前元素进行比较,检查其是否满足特定条件。如果是,则选择它一种方法是这样做
int index = 0;
var a = from i in someday
let indexNow = index++
where indexNow >= 3
let j = someday[indexNow - 3]
where i.Count > j.Count
select i;
创建临时变量j以在三步之前获取元素,然后将其与当前元素进行比较,检查其是否满足特定条件。如果是,则选择它使用索引的
Where
-重载,如下所示:
var result = myDates.Where((x, index) => index >= 3 && x > myDates.ElementAt(x - 3).Count);
这将从您的集合中选择所有计数大于三天前元素计数的元素。使用索引
,其中
-重载,如下所示:
var result = myDates.Where((x, index) => index >= 3 && x > myDates.ElementAt(x - 3).Count);
这将从您的集合中选择所有计数大于三天前元素计数的元素。虽然其他答案中描述的索引技术会起作用,但如果源序列不是基于列表的,它们将效率低下,在这种情况下,
ElementAt
将导致O(N^2)时间复杂度操作
对于O(N)时间复杂度(如果源序列本身不包含繁重的操作),一种可能更好的方法是使用and的组合,如下所示
var result = myDates
.Skip(3)
.Zip(myDates, (current, compare) => current.Count > compare.Count ? current : null)
.Where(item => item != null);
虽然在其他答案中描述的索引技术可以工作,但如果源序列不是基于列表的,则它们将是低效的,在这种情况下,
ElementAt
将导致O(N^2)时间复杂度操作
对于O(N)时间复杂度(如果源序列本身不包含繁重的操作),一种可能更好的方法是使用and的组合,如下所示
var result = myDates
.Skip(3)
.Zip(myDates, (current, compare) => current.Count > compare.Count ? current : null)
.Where(item => item != null);
你能澄清你的问题,或者使用不同的措辞吗?很难理解你想要什么。你能澄清你的问题,或者使用不同的措辞吗?很难理解你想要什么。