score:6
If you want to check if i.list
contains either "first"
or "second"
:
var val = new [] { "first", "second" };
var temp = from i in items
where val.Any (i.list.Contains)
select i;
If you want to check if i.list
contains both "first"
or "second"
:
var val = new [] { "first", "second" };
var temp = from i in items
where val.All (i.list.Contains)
select i;
However if performance is crucial (think called in a loop for hundreds of items), it would be more appropriate to use HashSet
intersection as advised by Hermit.
score:5
var temp = from i in items
where i.list.Any(x => val.Contains(x))
select i;
use All
if all list items should be in values
score:3
I am really bad at LINQ so I am not going to address that.
First doing a Contains on a list isn't the fastest thing. Doing a LINQ on list isn't doing to make it any faster. What you need to do is have a HashSet and then do a Contains. if you have two lists, i'd say create two HashSets and Intersect them.
http://msdn.microsoft.com/en-us/library/vstudio/bb918911(v=vs.90).aspx
score:1
Not sure what you mean by 'contains', if you want all of the Items that match, @lazyberezovsky's answer should be correct.
However, if you want to overrride IList.Contains to support an array (or Enumerable) you can do this:
/// <summary>
/// Return true if <paramref name="allItems"/>
/// contains one or more <paramref name="candidates"/>
/// </summary>
public static bool Contains<T>(IList<T> allItems, IEnumerable<T> candidates)
{
if (null == allItems)
return false;
if (null == candidates)
return false;
return allItems.Any(i => candidates.Contains(i));
}
score:1
Here's an extension method to get whether any of the items in the array exist in the list. This one returns a bool
like IList.Contains
.
public static class IListExtensions
{
public static bool ContainsAny<T>(this IList<T> list, IEnumerable<T> enumerable)
{
foreach (var item in enumerable)
{
if (list.Contains(item))
return true;
}
return false;
}
}
Usage:
IList<int> a = // ...
string[] b = // ...
a.ContainsAny(b);
Source: stackoverflow.com
Related Articles
- System.ArgumentException: Complex DataBinding accepts as a data source either an IList or an IListSource
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- The source contains no DataRows
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- Entity Framework 6 Code First Custom Functions
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Is it possible to express this code in LINQ?
- Find item in IList with LINQ
- I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- How to create a combobox into a dgv using entity framework and linq
- The entity or complex type 'WebApi.DbConnection.LoginCrediential' cannot be constructed in a LINQ to Entities query
- How can you tell if an Xnode is of XElement type?
- LINQ Sum of prices in each month in loop?
- Match and Map fields from two lists LINQ
- Linq Multiple Joins - Too many results
- Using ToList() on Enumerable LINQ query results for large data sets - Efficiency Issue?
- Is it possible to implement ExpressionTree.GreaterThan, etc for string so that LINQ can use it
- How do you use linq to xml to find matching nodes in two different xml files
- Update class property inside Enumerable.Zip linq c#
- Issue with building multiselect user control
- Nested sorting for predefined filters using LINQ and RavenDB
- How to return first object of a collection from its parent
- Performance comparison Sql query and Linq data query
- How to add a data-attribute to a dropdown menu with C#
- Using index in SelectMany LINQ
- Linq to SQL vs Entity Framework, Microsoft support for
- Dynamic linq Building Expression
- Use linq to combine 2 classes into a 3rd collection
- LINQ Query takes too long