Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
接收linq语句中当前比较的索引 til.First(t=>ViewModel.StringValue.CompareTo(t.StringValue)_Linq_Lambda - Fatal编程技术网

接收linq语句中当前比较的索引 til.First(t=>ViewModel.StringValue.CompareTo(t.StringValue)

接收linq语句中当前比较的索引 til.First(t=>ViewModel.StringValue.CompareTo(t.StringValue),linq,lambda,Linq,Lambda,不是直接与First比较,而是使用Select/First,是(在该示例中,您将获得一个匿名对象) 而strValue将是 res.index 这将为您提供一个匿名对象,其中包含一个StringValue和一个index属性您可以做的一件事是添加一个中介.Select(),它将带来一个匿名类型的对象,该对象包含当前t对象及其索引: res.StrValue til .Select((t,index)=>new{t,index}) //索引可在下一行中找到 .First(x=>ViewMode

不是直接与
First
比较,而是使用Select/First,是(在该示例中,您将获得一个匿名对象)

而strValue将是

res.index

这将为您提供一个匿名对象,其中包含一个
StringValue
和一个
index
属性您可以做的一件事是添加一个中介
.Select()
,它将带来一个匿名类型的对象,该对象包含当前
t
对象及其索引:

res.StrValue
til
.Select((t,index)=>new{t,index})
//索引可在下一行中找到

.First(x=>ViewModel.StringValue.CompareTo(x.t.StringValue)@RaphaëlAlthaus OP说他需要“在比较时获取t的索引值”,所以我假设在
.First()
lambda中有可用的索引就可以了。是的,但是如果在调用
.First(xxxx)后直接执行
t.StringValue
,您将得不到它。@RaphaëlAlthaus再说一次,我假设OP只需要调用
中的索引。首先,可能是记录它或用它执行其他查找。
res.index
res.StrValue
til
  .Select((t, index) => new {t, index})
    // x.index is available on the next line
  .First(x => ViewModel.StringValue.CompareTo(x.t.StringValue) <= 0)
  .t.StringValue;