WebTo get the last item of a collection use LastOrDefault () and Last () extension methods var lastItem = integerList.LastOrDefault (); OR var lastItem = integerList.Last (); Remeber to add using System.Linq;, or this method won't be available. Share Improve this answer Follow edited Apr 21, 2014 at 20:01 Almo 15.5k 13 69 95 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);
Randy Haid - Vancouver, Washington, United States Professional ...
WebDec 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 … Webpublic item FindItem (int i) { return itemList.FirstOrDefault (item => item.itemId == i); } FirstOrDefault is described as Returns the first element of a sequence, or a default value … chainring guard 44t
c# - how to check if List element contains an item with a …
WebYou can use find with a Predicate as follows: list.Find (x => x.Id == IdToFind); This will return the first object in the list which meets the conditions defined by the predicate (ie in … Web23 Most often we find generic list with code like: CartItem Item = Items.Find (c => c.ProductID == ProductID); Item.Quantity = Quantity; Item.Price = Price; So the above code finds and updates with other data, but if I want to find by multiple conditions, then how do I write the code? I want to write code like: WebJun 15, 2010 · Well, it depends on what version C# and .NET you are on, for 3.5 you could do it with LINQ: var qualiyfyingRows = from row in rows where Equals(row["MyColumn"], value) select row; // We can see if we found any at all through. bool valueFound = qualifyingRows.FirstOrDefault() != null; chainring bicycle surly