WebIf you have a list and you want to know where within the list an element exists that matches a given criteria, you can use the FindIndex instance method. Such as . int index = list.FindIndex(f => f.Bar == 17); Where f => f.Bar == 17 is a predicate with the matching criteria. In your case you might write WebApr 7, 2024 · I have a model with list items: public class Student{ public int StudentId { get; set; } public int ClassId { get; set; } } The table values are similar to the following: StudentId ClassI...
c# - LINQ select List where sub-list contains item from another list ...
WebFeb 8, 2014 · I'll suggest: var searchIds = new List {1,2,3,4,5}; var result = persons.Where (p => p.Locations.Any (l => searchIds.Contains (l.Id))); Contains will be translated to IN statement. Keep in mind that the id list goes into the sql statement. If your id list is huge then you'll end up having a huge query. Share. WebYou can check if a list is inside of another list with this var list1 = new List { 1, 2, 3, 4, 6 }; var list2 = new List { 2, 3 }; bool a = list1.Any (c => list2.Contains (c)); Share Improve this answer Follow edited Mar 22, 2024 at 19:18 Abra 18.6k 6 32 41 answered Mar 22, 2024 at 19:05 Pperez 61 1 2 Add a comment 0 boston house medical centre
c# - Check if list contains element that contains a string and get …
WebMar 5, 2016 · 3 Answers. Sorted by: 18. You can just use following LINQ expression: List1.Where (p => p.Cats.Any (c => List2.Any (c2 => c2.ID == c.ID))); You should also be able to do that with intersect (That is if your classes have their Equals methods overriden to check for matching IDs - see Intersect on MSDN ): WebFeb 1, 2009 · As I needed to check if there are items from a list in a (long) string, I ended up with this one: listOfStrings.Any (x => myString.ToUpper ().Contains (x.ToUpper ())); Or in vb.net: listOfStrings.Any (Function (x) myString.ToUpper ().Contains (x.ToUpper ())) Share Follow answered Oct 15, 2024 at 12:26 LiliumCandidum 61 8 Add a comment 2 WebFeb 28, 2024 · The ways to do this is by using a combination of Any and All. You need to check if all the elements of wordsToFind are substring of any elements in StringList bool result = wordsToFind.All (word => currentObject.StringList.Any (str => str.Contains (word)); This is for one object out of the list of Objects. You can again apply All to that list. hawkins brown careers