C# 过滤列表,并获取额外的元素
我有一个列表,我需要使用where子句对其进行过滤,但也需要使用前面的一个元素。范例C# 过滤列表,并获取额外的元素,c#,linq,C#,Linq,我有一个列表,我需要使用where子句对其进行过滤,但也需要使用前面的一个元素。范例 class xyz { DateTime TimeStamp; String a; } listVariable.OrderBy(x=>x.TimeStamp)。其中(x=>x.TimeStamp>= StartDateTime.LocalDateTime&&x.时间戳< EndDateTime.LocalDateTime).ToList(); 我需要的项目,以满足上述条件,我还需要采取一个额外的前元素(即
class xyz
{
DateTime TimeStamp;
String a;
}
listVariable.OrderBy(x=>x.TimeStamp)。其中(x=>x.TimeStamp>=
StartDateTime.LocalDateTime&&x.时间戳<
EndDateTime.LocalDateTime).ToList();
我需要的项目,以满足上述条件,我还需要采取一个额外的前元素(即元素之前,满足此条件)
x=>x.Timestamp>=StartDateTime.LocalDateTime)。我怎样才能做到这一点
该列表包含如下元素
“2018年11月9日”'10/11/2018' '15/11/2018' '18/11/2018' '21/11/2018' “2018年11月25日” 如果我查询18-20之间的日期。我应该 “2018年11月15日”->之前的一个额外元素
“2018年11月18日”“2018年11月20日”->这些是满足where(…)条件的集合。您可以创建两个集合,然后合并它们:
listVariable.OrderBy(x=>x.TimeStamp).Where(x => x.Timestamp >=
StartDateTime.LocalDateTime && x.Timestamp <
EndDateTime.LocalDateTime).ToList();
var list1=listVariable.OrderBy(x=>x.TimeStamp)。其中(x=>x.TimeStamp>=
StartDateTime.LocalDateTime&&x.时间戳<
EndDateTime.LocalDateTime).ToList();
var minItem=list1.Min(x=>x.TimeStamp);
var list2=listVariable.Where(x.Timestampx.Timestamp.First();
var finalList=list.Concat(list2);
您可以创建两个集合,然后合并它们:
listVariable.OrderBy(x=>x.TimeStamp).Where(x => x.Timestamp >=
StartDateTime.LocalDateTime && x.Timestamp <
EndDateTime.LocalDateTime).ToList();
var list1=listVariable.OrderBy(x=>x.TimeStamp)。其中(x=>x.TimeStamp>=
StartDateTime.LocalDateTime&&x.时间戳<
EndDateTime.LocalDateTime).ToList();
var minItem=list1.Min(x=>x.TimeStamp);
var list2=listVariable.Where(x.Timestampx.Timestamp.First();
var finalList=list.Concat(list2);
您可以使用库
为此,它具有超前和滞后功能
var list1 = listVariable.OrderBy(x=>x.TimeStamp).Where(x => x.Timestamp >=
StartDateTime.LocalDateTime && x.Timestamp <
EndDateTime.LocalDateTime).ToList();
var minItem = list1.Min(x=>x.TimeStamp);
var list2 = listVariable.Where(x.Timestamp < minItem.LocalDateTime).OrderBy(x=>x.TimeStamp).First();
var finalList = list.Concat(list2);
公共静态IEnumerable滞后(
这是一个数不清的来源,
整数偏移,
TSource defaultLagValue,
Func结果选择器
)
还请注意,此过程是资源密集型的您可以使用该库
为此,它具有超前和滞后功能
var list1 = listVariable.OrderBy(x=>x.TimeStamp).Where(x => x.Timestamp >=
StartDateTime.LocalDateTime && x.Timestamp <
EndDateTime.LocalDateTime).ToList();
var minItem = list1.Min(x=>x.TimeStamp);
var list2 = listVariable.Where(x.Timestamp < minItem.LocalDateTime).OrderBy(x=>x.TimeStamp).First();
var finalList = list.Concat(list2);
公共静态IEnumerable滞后(
这是一个数不清的来源,
整数偏移,
TSource defaultLagValue,
Func结果选择器
)
还请注意,这个过程是资源密集型的在这种情况下,我编写了自己的简单扩展方法(因为它很有趣,并且减少了对外部库的依赖)
公共静态类MyExtensions
{
公共静态IEnumerable SkipButOneWhile(此IEnumerable源,Func谓词)
{
使用(var e=source.GetEnumerator())
{
var=false;
var前导=默认值(T);
T电流;
while(true)
{
如果(!e.MoveNext())产生中断;
电流=e.电流;
if(谓词(当前))
{
hasLeading=true;
超前=电流;
}
其他的
打破
}
if(hasLeading)
收益率领先;
产生回流电流;
while(如MoveNext())
收益率,收益率,电流;
}
}
}
例如:
public static class MyExtensions
{
public static IEnumerable<T> SkipButOneWhile<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
using (var e = source.GetEnumerator())
{
var hasLeading = false;
var leading = default(T);
T current;
while (true)
{
if (!e.MoveNext()) yield break;
current = e.Current;
if (predicate(current))
{
hasLeading = true;
leading = current;
}
else
break;
}
if (hasLeading)
yield return leading;
yield return current;
while (e.MoveNext())
yield return e.Current;
}
}
}
listVariable
.OrderBy(x=>x.TimeStamp)
.SkipButOneWhile(x=>x.TimeStampx.TimeStamp
(注意:如果您知道它是经过排序的,那么,
TakeWhile
比Where
更有效,因为它允许您在跨越结束日期后立即停止枚举。)在这种情况下,我编写了自己的简单扩展方法(因为它很有趣,并且减少了对外部库的依赖性)
公共静态类MyExtensions
{
公共静态IEnumerable SkipButOneWhile(此IEnumerable源,Func谓词)
{
使用(var e=source.GetEnumerator())
{
var=false;
var前导=默认值(T);
T电流;
while(true)
{
如果(!e.MoveNext())产生中断;
电流=e.电流;
if(谓词(当前))
{
hasLeading=true;
超前=电流;
}
其他的
打破
}
if(hasLeading)
收益率领先;
产生回流电流;
while(如MoveNext())
收益率,收益率,电流;
}
}
}
例如:
public static class MyExtensions
{
public static IEnumerable<T> SkipButOneWhile<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
using (var e = source.GetEnumerator())
{
var hasLeading = false;
var leading = default(T);
T current;
while (true)
{
if (!e.MoveNext()) yield break;
current = e.Current;
if (predicate(current))
{
hasLeading = true;
leading = current;
}
else
break;
}
if (hasLeading)
yield return leading;
yield return current;
while (e.MoveNext())
yield return e.Current;
}
}
}
listVariable
.OrderBy(x=>x.TimeStamp)
.SkipButOneWhile(x=>x.TimeStampx.TimeStamp
(注意:如果您知道它是排序的,那么,
TakeWhile
比Where
更有效,因为它允许您在跨越结束日期后立即停止枚举。)只是一个想法,但是,如果您也执行与过滤器相反的操作,那么您将得到另一个列表,您可以从中获取/复制?我提出了以下内容:var nearest=Listvariable.MinBy(date=>Math.Abs((date.Timestamp-StartDateTime.LocalDateTime).Ticks().FirstOrDefault()
ListVariable.Where(x=>x.Timestamp>=StartDateTime.LocalDateTime&&x.Timestampx.Timestamp.ToList()
只是个主意,但是,如果您也执行与筛选相反的操作,那么您将得到另一个列表,您可以从中获取/复制?我提出了以下建议:var nearest=Listvariable.MinBy(date