C# list contains all items in another list
Webstring _tags = "sport,football"; List tags = _tags.Split (',').ToList (); var teams = (from t in db.Tags where tags.All (x=> x.Contains (t.TagName)) select t.Teams).FirstOrDefault ().Select (i => i.TeamNames).ToList (); This should help get you the desired result Share Improve this answer Follow answered Mar 18, 2015 at 17:03 shat90 WebMay 21, 2024 · Let's clear up any confusion you have related to the LINQ methods Any(), All() and Contains(). They're extremely useful for querying (asking questions about) your data. I'll first explain how they work at a high level and then give an example of each one. Important: All three of these methods return a boolean (true/false). Your result will ...
C# list contains all items in another list
Did you know?
WebAug 4, 2013 · 4 Answers Sorted by: 4 This takes each part of ListA and compares it with ListB with SequenceEqual: bool containsSameSequence = ListA .Where ( (item, index) => index <= ListA.Count - ListB.Count) .Select ( (item, index) => ListA.Skip (index).Take (ListB.Count)) .Any (part => part.SequenceEqual (ListB)); Demo 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
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 ): WebNov 9, 2011 · Here's a slightly less friendly O (n+m) solution. This should be used if superset is HUGE. It avoids repeatedly enumerating superset. HashSet hashSet = new HashSet (superset); bool contained = subset.All (i => hashSet.Contains (i)); Share Improve this answer Follow edited Jan 2, 2009 at 21:42 answered Jan 2, 2009 at 21:35 …
WebSep 12, 2013 · @V.7 Because he only wants to know if one item in the list contains a substring. list.equals is not the correct tool for the job [ "abc", "def", "ghi" ] does contain "hi" the way the OP describes it. list.equals doesn't even take the correct datatypes. – 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.
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
fat albert and the cosby kids tv seriesWebFeb 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 freni group messinaWebApr 13, 2024 · C# : Does .NET have a way to check if List a contains all items in List b?To Access My Live Chat Page, On Google, Search for "hows tech developer connect"So ... freni group srlWebIf you override the equality of People then you can also use: peopleList2.Except(peopleList1) Except should be significantly faster than the Where(...Any) variant since it can put the second list into a hashtable.Where(...Any) has a runtime of O(peopleList1.Count * peopleList2.Count) whereas variants based on HashSet … fat albert and the cosby kids gang warsWebMar 31, 2015 · To see if one list contains the values in another use the List.Intersect [ ^] method and check against the Count Any property. E.g. C#. Expand . private void … frenkcastle.itWebOct 4, 2009 · Hope it helps ;-) You could also use another way. Override equals and use this. public bool ContainsAll (List a,List check) { list l = new List (check); … freni direct mount shimano dura aceWebMay 30, 2013 · And now result of my measurement. I generated 100 000 UserProfiles and 100 000 ids. Join took 32ms and .Where with .Contains took 2 minutes and 19 seconds! I used pure IEnumerable for this testing to prove my statement. If you use List instead of IEnumerable, .Where and .Contains will be faster. Anyway the difference is significant. frenin injection