Entity Framework - attribute IN Clause usage
int[] ids = new int[]{1,2,3,45,99};
using (DatabaseEntities db = new DatabaseEntities ())
{
return db.Licenses.Where(
i => i.license == mylicense
&& ids.Contains(i.number)
).ToList();
}
should work
Entity Framework Core - IN clause equivalent
Here is how I would do it in one query based on the information you provided.
var posts = await _context.Post
.Where(post =>
_context.BlogPost.Any(bp => bp.BlogId == blogId && bp.PostId == post.PostId)
)
.ToListAsync();
Here is how I would do it in two queries in order to use Contains
based on the information you provided.
var postIds = await _context.BlogPost
.Where(bp => bp.BlogId = blogId)
.Select(bp => bp.PostId)
.ToArrayAsync();
var posts = await _context.Post
.Where(p => postIds.Contains(p.PostId))
.ToListAsync();
Here is how I would do it in one query if I were using valuable EntityFramework features, and I had a reference property named Post on BlogPost.
var posts = await _context.BlogPost
.Where(bp => bp.BlogId == blogId)
.Select(bp => bp.Post)
.ToListAsync();
Here is how I would do it in one query if I were using valuable EntityFramework features, and I had a collection property named Posts from Blog, and the many-many BlogPost table was hidden by EntityFramework in such a way that you never actually interact with it from C#.
var posts = await _context.Blog
.Where(b => b.BlogId == blogId)
.SelectMany(b => b.Posts)
.ToListAsync();
On the other hand if the many-many BlogPost table is exposed by EntityFramework then you could still start at the Blog and use properly configured collection and reference properties to get to the posts like so.
var posts = await _context.Blog
.Where(b => b.BlogId == blogId)
.SelectMany(b => b.BlogPosts)
.Select(bp => bp.Post)
.ToListAsync();
Or
var posts = await _context.Blog
.Where(b => b.BlogId == blogId)
.SelectMany(b => b.BlogPosts.Select(bp => bp.Post))
.ToListAsync();
Takeaway, EntityFramework is not SQL. What you do in SQL may or may not map directly, or even apply to how you would go about it in EntityFramework. Not only that but when you use EntityFramework you are using EntityFramework and C# language features that are not EntityFramework per se, LINQ for example. Breaking down the problem into its constituent parts can help you solve problems, and make it easier to study up to being able to do more complex operations. Studying and practicing LINQ in isolation will help you be better with EntityFramework.
How to do an in query in entity framework?
Try this:
var orderKeys = new int[] { 1, 12, 306, 284, 50047};
var orders = (from order in context.Orders
where orderKeys.Contains(order.Key);
select order).ToList();
Assert.AreEqual(orderKeys.Count, orders.Count);
Edit: I have found some workarounds for this issue - please see WHERE IN clause?:
The Entity Framework does not
currently support collection-valued
parameters ('statusesToFind' in your
example). To work around this
restriction, you can manually
construct an expression given a
sequence of values using the following
utility method:
Select WHERE IN in entity framework
You can rewrite your IN clause as EXISTS and use the following linq command:
var q = from p in dbContext.Providers
where !dbContext.ProviderTeams.Any(pt => pt.TeamRef == 134 && pt.providerRef == p.serial)
select p;
How can I query the foreign key objects using Entity Framework?
Just include the child entity you want to load:
using (var context = new MovieContext())
{
List<Movie> movieList = context.Movies
.Include(m => m.Revenue) // ADD THIS INCLUDE
.ToList();
Console.WriteLine("Movie Name: " + movieList[0].Name);
if (movieList[0].Revenue == null)
{
Console.WriteLine("Revenue is null!");
}
else
{
Console.WriteLine(movieList[0].Revenue.GrossIncome);
}
Console.ReadLine();
}
This will load the movies - and also make sure that all the references to their respective .Revenue
references have been loaded, too.
How to filter by fields from navigation properties in Entity Framework?
Try this
.SelectMany(x => x.ProductsInCategories.Where(c => descentantIds.Contains(c.ProductCategoryID))).Select(c => c.Product).Distinct()
Entity Framework: Unknown column in 'where clause
your join is "weird". How is equals
evaluating someMovie == anotherMovie
??
why don't you just:
var movies = (from
s in db.Shows
where
s.StartTime.Year == todaysDate.Year &&
s.StartTime.Month == todaysDate.Month &&
s.StartTime.Day == todaysDate.Day
select
s.Movie).Distinct().ToList();
var shows = (from
s in db.Shows
where
s.StartTime.Year == todaysDate.Year &&
s.StartTime.Month == todaysDate.Month &&
s.StartTime.Day == todaysDate.Day
select
s).ToList();
or
var shows = (from
s in db.Shows.Include(x => x.Movies)
where
s.StartTime.Year == todaysDate.Year &&
s.StartTime.Month == todaysDate.Month &&
s.StartTime.Day == todaysDate.Day
select
s).ToList();
var movies = shows.Select(x => x.Movie).Distinct().ToList();
Related Topics
Regular Expression in Postgresql Like Clause
How to Make a Parametrized SQL Query on Classic Asp
Is There a Select ... into Outfile Equivalent in SQL Server Management Studio
Generate SQL Insert Script from Excel Worksheet
Mysql: How to Copy Rows, But Change a Few Fields
How to Do Multiple Case When Conditions Using SQL Server 2008
Difference Between Right & Left Join VS Right & Left Outer Join in SQL
Find the Real Column Name of an Alias Used in a View
How to Map Input and Output Columns Dynamically in Ssis
Select from Table with Varying in List in Where Clause
How to Split String by Character into Separate Columns in SQL Server
Using Pivot in SQL Server 2008
SQL to Add Column with Default Value - Access 2003
Connect Different Windows User in SQL Server Management Studio (2005 or Later)