How can I get the index using linq? I want to find FieldNo and go back to the index. let's say if I'm looking for 2, it should be a return index.
Hi,
With LINQ:
int index = fields.Select((f, i) => new { Field = f, Index = i}) .Where(x => x.Field.FieldNo == 2) .Select(x => x.Index) .DefaultIfEmpty(-1) .FirstOrDefault();
without LINQ, using List.FindIndex, more readable, efficient and even works on .NET 2 :
List.FindIndex
int index = fields.FindIndex(f => f.FieldNo == 2);
If I understand your question correctly, this is what you need:
Field field = Field.Where(x => x.FieldNo == 2).FirstOrDefault(); if (field != null) { Field.IndexOf(field); }
, overload .Select,
overload
.Select
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector);
:
List.Select((t, index) => t );
, , ,
Source: https://habr.com/ru/post/1534774/More articles:How to ignore Jackson annotations? - javaSaving CSS class in object in localstorage on reload page - javascriptHow to plot a 95 percent and 5 percent chart on a ggplot2 chart with already calculated values? - rVarious JSON settings in a Spring application for serializing REST and Ajax - javaHow to reset an enlarged image to the original image - androidHow to reset the image to its original position - androidSympy: Conjugate real functions - pythonMaven-war-plugin doubles manifest.mf - javahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1534779/python-functions-access-to-global-variables&usg=ALkJrhjpc8IVLH2uK9mykZ2Mhm83LSVXagFor loop and sleep in bash script - linuxAll Articles