linq where list contains any in list
Sounds like you want:
var movies = _db.Movies.Where(p => p.Genres.Intersect(listOfGenres).Any());
LINQ query to find if items in a list are contained in another list
var test2NotInTest1 = test2.Where(t2 => test1.Count(t1 => t2.Contains(t1))==0);
Faster version as per Tim's suggestion:
var test2NotInTest1 = test2.Where(t2 => !test1.Any(t1 => t2.Contains(t1)));
Linq Where list Contains item in another list
You can use Any
+ Contains
:
var query = workOrderItems
.Where(item => item.Jobs.Any(j => longList.Contains(j.ResultElement.Id)));
( presuming that the class Element
has an Id
property since you've stated ResultElementid )
Where list contains any in List
Ok, can you do:
var thelist = _context.Products.Where(n => ListProducts.Contains(n.ProductId)).Select(n => new ProductVM
{
Name = n.Name,
Quantity = n.Quantity
}).ToList();
Use LINQ where list of items contains any component in a list of components
It should be the other way around: first the Any
and then the Contains
. You would like to get all items that have any of there components contained in the components
list.
Also don't use the Select
if you'd like to get a list of Item
back:
var result = items.Where(i => i.Components.Any(c => components.Contains(c.Name)));
Check if text contains any string item from list of string c# linq
I'm not sure this is possible in a single statement like this. It's too complicated for the poor parsing stuff to work out.
However, you can get an IQueryable(), then iterate over your filters append these as individual WHERE clauses, then these should get added to the SQL properly later.
Something like this:
//this just gets a reference the DbSet, which implements IQueryable<User>
var queryable = _dbContext.Users;
//iterate over the filters and add each as a separate WHERE clause
foreach(var f in filters)
{
//this just adds to the existing expression tree..
queryable = queryable.Where(u=>u.Name.Contains(f));
}
//this will actually hit the database.
var results = queryable.ToList();
This should generate something like this in SQL (entirely pseudo-code)
select
u.*
from
users u
where
(u.username like "%Sue%")
or (u.username like "%Bob%")
Hope this helps...
Check if listt contains any of another list
You could use a nested Any()
for this check which is available on any Enumerable
:
bool hasMatch = myStrings.Any(x => parameters.Any(y => y.source == x));
Faster performing on larger collections would be to project parameters
to source
and then use Intersect
which internally uses a HashSet<T>
so instead of O(n^2) for the first approach (the equivalent of two nested loops) you can do the check in O(n) :
bool hasMatch = parameters.Select(x => x.source)
.Intersect(myStrings)
.Any();
Also as a side comment you should capitalize your class names and property names to conform with the C# style guidelines.
Related Topics
Does Using Tasks (Tpl) Library Make an Application Multithreaded
Getting Checkbox Value in ASP.NET MVC 4
What's the Correct Alternative to Static Method Inheritance
Two-Way/Bidirectional Dictionary in C#
Optimal Way to Read an Excel File (.Xls/.Xlsx)
How to Create a Xsd Schema from a Class
How to Use the C#6 "Using Static" Feature
How to Stop Flickering C# Winforms
How Do C# Events Work Behind the Scenes
How to Provide Custom Cast Support for My Class
Illustrating Usage of the Volatile Keyword in C#
Pattern for Calling Wcf Service Using Async/Await
Detecting Network Connection Speed and Bandwidth Usage in C#
How to Get the Width and Height of a Multi-Dimensional Array
Convert String to Hex-String in C#