LINQ/Lambda - Getting parent object property while using Include to obtain children - c#

I am using this "model" from a previous question. Here I have a Vehicle,VehicleType, and Price entity.
public class Vehicle
{
public int Id {get; set;}
public string Name {get; set}
public int VehicleTypeId {get; set;}
public virtual VehicleType VehicleType {get; set;}
}
public class VehicleType
{
public int Id {get; set;}
public string VehicleTypeName {get; set}
public ICollection<Vehicle> Vehicles {get; set;}
public ICollection<Price> Prices {get; set;}
}
public class Price
{
public int Id {get; set;}
public int Price {get; set;}
public int VehicleTypeId {get; set;}
public virtual VehicleType VehicleType {get; set;}
}
The VehicleType entity serves as a relationship to both the Vehicle entity and the Price entity. I am trying to use LINQ to get at the Price for a given Vehicle based upon it's VehicleType...
// Query #1 to get `Vehicle` name
var vehicle = dbContext.Vehicles.SingeOrDefault(v => v.Id = 1234);
string vehicleName = vehicle.Name;
// Query #2 to get lowest `Price` for `Vehicle`
var myVehiclePrice = dbContext.Vehicles.Include("VehicleType.Prices")
.SingleOrDefault(v => v.Id == 1234)
.VehicleType.Prices
.OrderBy(p => p.PriceAmount).FirstOrDefault()
I have two queries to get the information I want. Is there a way to combine these two queries together to make one trip to the database? I tried reusing the vehicle variable obtained from the first query, but it represents a single entity and cannot make use of the Include() extension which only works off of an object query.

I think you are misinterpreting the Include. It's used for eagerly loading the entity content. It's not needed at all for queries that use selective projection of entity properties/aggregates.
You can gather the vehicle name and the lowest price with a single database trip simply with
var info = dbContext.Vehicles
.Where(v => v.Id == 1234)
.Select(v => new
{
VehicleName = v.Name,
LowestPrice = v.VehicleType.Prices.Min(p => (int?)p.Price)
})
.SingleOrDefault();

Related

How to get records from table that depends on another tables in entity framework core?

I want to get some records from Database that depends on three tables.
Three tables are:
1.Company(Id,Name)
2. Car(Id,CompanyId,Name)
3. Showroom(Id,CarId,Name)
Now a one company contains many cars and many cars may exist in many showrooms.
I want to get records from showroom table where company '2' cars exist along with cars. Is it possible to do it in entity framework core?
I think your entities will be like :
Company
public class Company
{
public int Id {get; set;}
public string Name {get; set;}
public ICollection<Car> Cars {get; set;}
}
Car:
public class Car
{
public int Id{get; set;}
public string Name {get; set;}
public int CompanyId{get; set;}
public Company Company {get; set;}
}
ShowRoom:
public class ShowRoom
{
public int Id{get; set;}
public string Name {get; set;}
public int CarId{get; set;}
public Car Car{get; set;}
}
In your method:
var context = new SomeContext();
var showRooms= context.ShowRooms
.Include(x=> x.Car)
.ThenInclude(x=> x.Company)
.Where(x=> x.Car.Company.Id== 2)
.ToList();

Filter data with EF Core in a single query

I'm trying to filter data in ASP.NET Core 2.1 using EF Core DbContext. The scenario is as follows:
Our main entity is a Movie. A movie has one or more genres and a genre can belong to one or more movies. The same goes for movie and actor. So we have two many-to-many relationships(code first) and the code that describes these is:
public class Movie
{
public int Id {get; set;}
public string Title {get; set;}
public ICollection<MovieActor> MovieActors {get; set;}
public ICollection<MovieGenre> MovieGenres {get; set;}
}
public class Actor
{
public int Id {get; set;}
public string Name {get; set;}
public ICollection<MovieActor> MovieActors {get; set;}
}
public class Genre
{
public int Id {get; set;}
public string Name {get; set;}
public ICollection<MovieGenre> MovieGenres {get; set;}
}
public class MovieActor
{
public int MovieId {get; set;}
public Movie Movie {get; set;}
public int ActorId {get; set;}
public Actor Actor {get; set;}
}
public class MovieGenre
{
public int MovieId {get; set;}
public Movie Movie {get; set;}
public int GenreId {get; set;}
public Genre Genre {get; set;}
}
The context responsible for handling queries with the database is MoviesDbContext.
I'm trying to filter all the data from the 'Movies' table based on two lists of ints, which represent the Ids of actors and genres in the database.
List<int> actorIds;
List<int> genreIds;
For filtering, we want to get all movies that simultaneously follow the following rules:
1) All movies whose list of actors contain at least one actor whose Id is found in the 'actorIds' list
2) All movies whose list of genres contain at least one genre whose Id is found in the 'genreIds' list
The solution that I found is as follows:
context.Movies
.Include(m => m.MovieActors)
.Include(m => m.MovieGenres)
.Where(m => actorIds.Any(id => m.MovieActors.Any(ma => ma.Id == id)))
.Where(m => genreIds.Any(id => m.MovieGenres.Any(mg => mg.Id == id)));
This does the filtering right, but the problem is that when EF Core translates the code into sql commands, it breaks the query into a lot of tiny queries and this causes severe performance issues, some queries taking tens of seconds.
How can I refactor this so that it does the filtering in only one query?
After some experiments with the extension methods, I've found something that does only N + 1 queries instead of thousands of queries, where N is the number of many-to-many relationships.
So, instead of using this lambda in the Where() extension method:
m => actorIds.Any(id => m.MovieActors.Any(ma => ma.Id == id))
You have to use this one:
m => m.MovieActors.Any(ma => actorIds.Contains(ma.ActorId))
After doing some research, I found that EF Core is still incomplete and cannot overcome the N + 1 problem. But still, N + 1 queries instead of a few thousands of queries is a huge improvement.

How to return this result using linq in entity framework( 2 foreign key mapping table)

I have a table that contains 2 foreign key that reference separately to 2 different table.
I would like to return the result of all person that has course of "Science".
How to retrieve the record back using LINQ?
This is what i gotten so far:
return
_ctx.Person
.Include(u => u.Course
.Where(ug=>ug.CourseName== "Science"));
This is not working as it shows the error.
The Include path expression must refer to a navigation property
defined on the type
public class Course
{
public int CourseID {get; set;}
public string CourseName {get; set;}
public virtual ICollection<Person> Persons { get; set; }
}
public class Person
{
public int PersonID {get; set;}
public virtual ICollection<Course> Courses { get; set; }
}
This is the mapping table. Only contains 2 foreign key from 2 different table.
I could not use this table inside the solution.As the code first won't generate this table as it doesn't contain it's own PK.
//This is not shown in the EntityFramework when generating Code First.
public class PersonCouseMap
{
public int PersonID {get; set;}
public int CourseID {get; set;}
}
Update : this works after I switched the entity.
return _ctx.Course
.Include(u=>u.Person)
.Where(ug=>ug.CourseName == "Sciene");
Anyone can explain why it won't work the another way round.
I need to display a List of Person who have course of "Science",
not Course Science that has a list of user.
The original query does not work because you've pushed the Where predicate inside the Include expression, which is not supported as indicated by the exception message.
The Include method is EF specific extension method used to eager load related data. It has nothing to do with the query filtering.
To apply the desired filter person that has course of "Science" you need Any based predicate since the Person.Courses is a collection:
return _ctx.Person
.Where(p => p.Courses.Any(c => c.CourseName == "Science"));
To include the related data in the result, combine it with Include call(s):
return _ctx.Person
.Include(p => p.Courses)
.Where(p => p.Courses.Any(c => c.CourseName == "Science"));
It looks like there is no relations between these two entites, you can establish a relationship by making the following changes to your code:
Here I am assuming that you want to establish Many-to-Many relationship between these two tables by having a third entity PersonCourseMap
public class Course
{
public int CourseID {get; set;}
public string CourseName {get; set;}
public virtual ICollection<CoursePersons> Courses { get; set; }
}
public class Person
{
public int PersonID {get; set;}
public virtual ICollection<PersonCourse> Courses { get; set; }
}
public class PersonCourseMap
{
public int PersonID {get; set;}
public int CourseID {get; set;}
public virtual ICollection<Person> Persons { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
After making above changes you can simply navigate through properties.
Include Foreign Key Mapping
public class Course
{
public int CourseID {get; set;}
public string CourseName {get; set;}
public virtual ICollection<Person> Person {get; set}
}
public class Person
{
public int PersonID {get; set;}
public virtual ICollection<Course> Course {get; set;}
}
using System.ComponentModel.DataAnnotation.Schema;
public class PersonCouseMap
{
[ForeignKey("Person")]
public int PersonID {get; set;}
[ForeignKey("Course")]
public int CourseID {get; set;}
public virtual ICollection<Person> Person {get; set;}
public virtual ICollection<Course> Course {get; set;}
}

How to update related entities using GraphDiff?

I have the following model:
public class Customer
{
public int Id {get; set;}
public string Name {get; set;}
public int AddressId {get; set;}
public virtual Address Address {get; set;}
public virtual ICollection<CustomerCategory> Categories {get; set;}
}
public class CustomerCategory
{
public int Id {get; set;}
public int CustomerId {get; set;}
public int CategoryId {get; set;}
public virtual Category Category {get; set;}
}
public class Address
{
public int Id {get; set;}
public string Street{get; set;}
public virtual PostCode PostCode {get; set;}
}
From the above, and using GraphDiff, I want to update the customer aggregate as follows:
dbContext.UpdateGraph<Customer>(entity,
map => map.AssociatedEntity(x => x.Address)
.OwnedCollection(x => x.Categories, with => with.AssociatedEntity(x => x.Category)));
But the above is not updating anything!!
What is the correct way to use GraphDiff in this case?
GraphDiff basically distinguishes two kinds of relations: owned and associated.
Owned can be interpreted as "being a part of" meaning that anything that is owned will be inserted/updated/deleted with its owner.
The other kind of relation handled by GraphDiff is associated which means that only relations to, but not the associated entities themselves are changed by GraphDiff when updating a graph.
When you use the AssociatedEntity method, the State of the child entity is not part of the aggregate, in other words, the changes that you did over the child entity will not be saved, just it will update the parent navegation property.
Use the OwnedEntity method if you want to save tha changes over the child entity, so, I suggest you try this:
dbContext.UpdateGraph<Customer>(entity, map => map.OwnedEntity(x => x.Address)
.OwnedCollection(x => x.Categories, with => with.OwnedEntity(x => x.Category)));
dbContext.SaveChanges();

Entity framework and Classes

I am trying to learn Entity framework. Say, I have the following classes
class Course
{
[Key]
public virtual int CourseID {get; set;}
public virtual string CourseName {get; set;}
}
class CourseDBContext:DBContext
{
public DbSet<Course> Courses{get;set;}
}
Then I can use Linq to query the database as shown below
using (CourseDBContext a = new CourseDBContext())
{
var b = from c in a.Course
where c.CourseID == 1001
select c;
var d = b.FirstOrDefault();
if(d != null)
Console.WriteLine(d.CourseName);
}
This works fine. Now if I add a second class
class Assignment
{
[Key]
public virtual int CourseID {get; set;}
public virtual int StaffID {get; set;}
}
class AssignmentDBContext:DBContext
{
public DbSet<Assignment> Assignments{get;set;}
}
Now, How can I use Linq to select and display the CourseName and StaffID associated with CourseID = 1001?
The example above is contrived and so the table design and fields are irrelevant. I just want to know how to query the data between two classes from two different database tables using Entity Framework and Linq.
Thanks
Both entities need to be in the same context.
public class CoursesContext: DbContext
{
public DbSet<Assignment> Assignments {get; set;}
public DbSet<Course> Courses {get; set;}
}
You can add an Assignment navigation property to filter on a foreign key:
public class Course
{
[Key]
public virtual int CourseID {get; set;}
public virtual string CourseName {get; set;}
public virtual Assignment {get; set;}
}
Then you can query like so:
var staffId =
from c in a.Course
where c.CourseID == 1001
select c.Assignment.StaffID;
Don't have a seperate context for each DbSet. I.e
class MyDbContext : DBContext
{
public DbSet<Course> Courses{get;set;}
public DbSet<Assignment> Assignments{get;set;}
}

Categories