目标元素的C#Linq最近索引
是否可以创建linq来查找C#4.0中目标元素的最近索引? 例如,我有一个数组目标元素的C#Linq最近索引,c#,linq,C#,Linq,是否可以创建linq来查找C#4.0中目标元素的最近索引? 例如,我有一个数组 string[] array=new string[] {"","","source","","source2","","","destination","source3","destination"}; “目的地”位于7和9,与2(“来源”)最近的“目的地”索引应为7,而不是9 如果条件是 int index(int sourceIndex,string[] array,string destination)
string[] array=new string[]
{"","","source","","source2","","","destination","source3","destination"};
“目的地”位于7和9,与2(“来源”)最近的“目的地”索引应为7,而不是9
如果条件是
int index(int sourceIndex,string[] array,string destination)
{
int temp=sourceIndex;
while(temp<array.Length)
{
if(array[sourceIndex]==destination)
return sourceIndex;
temp++;
}
return -1;
}
int索引(int-sourceIndex,string[]数组,string-destination)
{
int temp=源索引;
虽然(temp不一定是最有效的解决方案,但如果您不希望有很多匹配项,它应该可以工作:
string[] array = new string[] {"","","source","","source2","","","destination","source3","destination"};
var withIndex = array.Select((Value, Index) => new { Index, Value });
var result =
(from dest in withIndex.Where(_ => _.Value == "destination")
from source in withIndex.Where(_ => _.Value == "source")
let dif = dest.Index - source.Index
orderby dif
select dest.Index)
.FirstOrDefault();
我们首先分析源代码和目标代码的出现情况,然后找出最小的差异。不一定是最有效的解决方案,但如果您不希望出现大量匹配,则应该可以:
string[] array = new string[] {"","","source","","source2","","","destination","source3","destination"};
var withIndex = array.Select((Value, Index) => new { Index, Value });
var result =
(from dest in withIndex.Where(_ => _.Value == "destination")
from source in withIndex.Where(_ => _.Value == "source")
let dif = dest.Index - source.Index
orderby dif
select dest.Index)
.FirstOrDefault();
我们首先计算源
和目标
的出现次数,然后找出最小的差异。如果您确实有一个数组,我认为您不需要使用Linq
这样做要简单得多:
int startIndex = 2;
int nearest = Array.IndexOf(array, "destination", startIndex+1);
然后,最近的
将包含您正在查找的结果,如果找不到“目的地”
,则为-1
注意:这只查找前进方向上最近的,我假设这是您想要的,因为您的语句是:“源索引已知且在前进方向上最近,如果没有发生,则不需要返回”
还要注意的是,我使用了startIndex+1
,因为我们不需要从第一个位置开始搜索。如果您确实有一个数组,我认为您不需要使用Linq
这样做要简单得多:
int startIndex = 2;
int nearest = Array.IndexOf(array, "destination", startIndex+1);
然后,最近的
将包含您正在查找的结果,如果找不到“目的地”
,则为-1
注意:这只查找前进方向上最近的,我假设这是您想要的,因为您的语句是:“源索引已知且在前进方向上最近,如果没有发生,则不需要返回”
还请注意,我使用了startIndex+1
,因为我们不需要从第一个位置开始搜索。如果您真的想使用linq(假设您有IEnumerable
而不是真正的数组),您可以这样做:
static int index(int sourceIndex, IEnumerable<string> array, string destination)
{
return array
// convert items stream into (item, index) pair
.Select((item, index) => new { Item = item, Index = index })
// skip items until condition is met
.SkipWhile(c => c.Index <= sourceIndex || c.Item != destination)
// we need index only
.Select(c => c.Index)
// if we found nothing - return -1
.DefaultIfEmpty(-1)
.First();
}
静态int索引(int-sourceIndex、IEnumerable数组、字符串目标)
{
返回数组
//将项流转换为(项、索引)对
.Select((项,索引)=>new{item=item,index=index})
//跳过项目,直到满足条件
.SkipWhile(c=>c.索引c.索引)
//如果我们什么也没找到-返回-1
.DefaultIfEmpty(-1)
.First();
}
如果您有一个数组-请不要这样做,请使用Matthew Watson answer。如果您真的想使用linq(假设您有IEnumerable
而不是真正的数组),您可以这样做:
static int index(int sourceIndex, IEnumerable<string> array, string destination)
{
return array
// convert items stream into (item, index) pair
.Select((item, index) => new { Item = item, Index = index })
// skip items until condition is met
.SkipWhile(c => c.Index <= sourceIndex || c.Item != destination)
// we need index only
.Select(c => c.Index)
// if we found nothing - return -1
.DefaultIfEmpty(-1)
.First();
}
静态int索引(int-sourceIndex、IEnumerable数组、字符串目标)
{
返回数组
//将项流转换为(项、索引)对
.Select((项,索引)=>new{item=item,index=index})
//跳过项目,直到满足条件
.SkipWhile(c=>c.索引c.索引)
//如果我们什么也没找到-返回-1
.DefaultIfEmpty(-1)
.First();
}
如果你有一个数组,请不要这样做,使用Matthew Watson回答。< /P>在结果中<代码>源>代码>什么作用?不管怎样,你要求我们为你编写代码,通常不会提供代码编写服务。考虑这是一个温和的提醒:你真的需要使用LINQ来做这个吗?如果你看的话,你更喜欢哪个?查找距离“源3”最近的“目标”?数组中是否可能有多个“源”?
var index=array.IndexOf(items,“destination”,array.IndexOf(items,“source”))无论如何,你要我们为你编写代码,通常不会提供代码编写服务。考虑这是一个温和的提醒吗?你真的需要使用LINQ来做这个吗?如果你正在寻找最近的“目的地”到“SooCe3”,你更喜欢哪一个?数组中是否可能有多个“源”?var index=array.IndexOf(项,“目的地”,数组.IndexOf(项,“源”))
OP没有说开始索引必须在source
的索引之后。如果在source之前有一个目的地,在source之后有一个目的地呢?@TimSchmelter我相信他在评论中说过“在前进方向最近”这个方法比Linq快吗?我需要在一个datatable中为大约50个数据计算它,并为它创建一个新列并更新它。Thanks@noviceinDotNet要确定它是否更快,您应该仔细计算时间,但如果它没有明显快于Linq方法,我会非常惊讶SQL可能能够在SQL中执行操作,这可能会加快这种情况下的速度-就像我说的,你必须计时。OP没有说开始索引必须在source
的索引之后。如果在source之前有一个目的地,在source之后有一个目的地呢?@TimSchmelter我相信他在评论中说过-“在前进方向最近"这个方法比Linq快吗?我需要在一个datatable中为大约50个数据计算它,并为它创建一个新列并更新它。Thanks@noviceinDotNet要确定它是否更快,您应该仔细计算时间,但如果它没有明显快于Linq方法,我会非常惊讶SQL可能能够在SQL中执行操作,这可能会加快这种情况下的速度—就像我说的,您必须计时。