C# find list item
WebJul 1, 2009 · One option for the follow on question (how to find a customer who might have any number of first names): List names = new List { "John", "Max", "Pete" }; bool has = customers.Any (cus => names.Contains (cus.FirstName)); or to retrieve the customer from csv of similar list. WebYou can use the Find method on the generic list class. The find method takes a predicate that lets you filter/search the list for a single item. List list = // ..; CompareDesignGroup item = list.Find (c => c.FieldId == "SomeFieldId"); item will be null if there is no matching item in the list.
C# find list item
Did you know?
WebFeb 18, 2024 · C# void QueryHighScores(int exam, int score) { var highScores = from student in students where student.ExamScores [exam] > score select new { Name = student.FirstName, Score = student.ExamScores [exam] }; foreach (var item in highScores) { Console.WriteLine ($"{item.Name,-15}{item.Score}"); } } QueryHighScores (1, 90); WebAug 29, 2013 · The first approach uses a loop: bool isFound = false; foreach (item1 in list1) { if (list2.Contains (item1)) { isFound = true; break; } } The second one uses Linq directly: bool isFound = list1.Intersect (list2).Any (); The first one is long to write and not very straightforward/easy-to-read.
WebOct 3, 2014 · You can use the ElementAt extension method on the list. For example: // Get the first item from the list using System.Linq; var myList = new List { "Yes", "No", … WebDec 31, 2010 · Find is not optimized at all -- it performs a linear search, since that's the only thing that makes sense on an unsorted list. If you are looking at a nicer way to write it, …
WebJan 28, 2013 · public class PriceDetails { public float AverageNightlyRate { get; set; } } public class RoomContainer { public PriceDetails RoomPriceDetails { get; set; } public string PromotionDescription { get; set; } } public List HotelRooms { get; set; } The list HotelRooms has 10 items. WebList list = new List () { "a", "a", "b", "b", "r", "t" }; var dups = list.GroupBy (x => x) .Where (x => x.Count () > 1) .Select (x => x.Key) .ToList (); Share Improve this …
WebYou don't actually need LINQ for this because List provides a method that does exactly what you want: Find. Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List. Example code: PricePublicModel result = pricePublicList.Find(x => x.Size == 200);
WebJun 11, 2024 · Do you want the item in the list or the actual item itself (would assume the item itself). Here are a bunch of options for you: string result = _list.First(s => s == … bat barakahWebIf you just want to access the last item in the list you can do. if (integerList.Count > 0) { // pre C#8.0 : var item = integerList[integerList.Count - 1]; // C#8.0 : var item = … bat barletta andria traniWebJul 27, 2012 · Edit1: Depending on your use case, it might also make sense to maintain an "index" that maps from part IDs to cars. Something like: var partIDToCar = … tarako6180tarakonasWebJun 11, 2024 · How about the List.FindIndex Method: int index = myList.FindIndex (a => a.Prop == oProp); This method performs a linear search; therefore, this method is an O … tarako cdWebDec 20, 2010 · You should probably be using the FindAll method: List results = myClassList.FindAll (x => x.item1 == "abc"); Or, if you prefer your results to be typed as IEnumerable rather than List, you can use LINQ's Where method: IEnumerable results = myClassList.Where (x => x.item1 == "abc"); Share … batbareWebC# LINQ find duplicates in List (13 answers) Closed 3 years ago. I have: List list = new List () { "a", "a", "b", "b", "r", "t" }; How can I get only "a","b"? I tried to do like … bat barrel tumbler