简体   繁体   中英

LinQ Query from different tables

Is something like this possible?

var query = _context.Group
    .Where(g => g.StartDate > DateTime.Now)
    .Select(g => new GroupDTO {
        Name = g.GroupName,
        StartDate = g.StartDate,
        AvailableTeachers = _context.Teacher.Where(t => t.Available)
    });

Problem is, that the availableTeachers are not linked to the group in any way in the database - I would still store them into my DTO for later use - is this possible? I keep getting an error:

Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.SqlExpressions.SqlFunctionExpression' to type 'System.Linq.Expressions.ConstantExpression'.

UPDATE

This one works though and is so much better than what I tried:

var teachers = _context.Teacher.Where(t => t.Available).ToList();

var query = _context.Group
    .Where(g => g.StartDate > DateTime.Now)
    .Select(g => new GroupDTO {
        Name = g.GroupName,
        StartDate = g.StartDate,
        AvailableTeachers = teachers
    });

As teachers are not connected to groups in any way, you could optimize this by not including the request to teachers in the request for groups. As your statement is built now, you try to fetch the available teachers for each group again. If you have n groups, this means you will have n requests for teachers with the same results (if availability of the teachers does not change).

You can optimize this by splitting the requests, eg:

// Get the available teachers first (ToArray runs the query)
var teachers = _context.Teacher.Where(t => t.Available).ToArray(); 
// Get the groups
var query = _context.Group
    .Where(g => g.StartDate > DateTime.Now)
    .Select(g => new GroupDTO {
        Name = g.GroupName,
        StartDate = g.StartDate,
    })
    .ToArray()             // This runs the query against the db
    .Select(g => {
      AvailableTeachers = teachers;
      return g;
    })                     // This adds the teachers information after the db query
    .ToArray();

Above statement first reads the teachers from the database and stores them in a variable. After that, the groups are read from database. After the ToArray , the information is in memory and the list of available teachers is set.

// fetch all the list of teachers who are available stored in memory.
var teachers = _context.Teacher.Where(t => t.Available).ToList();

// to get the group record
var query = _context.Group
    .Where(g => g.StartDate > DateTime.Now)
    .Select(g => new GroupDTO {
        Name = g.GroupName,
        StartDate = g.StartDate,
        AvailableTeachers = teachers
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM