使用 Ef core 3.1 从 Db 获取相关数据
getting related data from Db with Ef core 3.1
我试图从 2 个相关的 table 中查询数据,但是(EfCore 3.1 中没有 groupJoin)结果为空,因为相关的 table 没有项目。
我正在使用 mvc 核心 3.1 和 EF 核心 3。
代码
var vmq =
from req in _context.Set<QuettaReq>()
.Where(sd => sd.SiteUserId == _userManager.GetUserId(User) && sd.QuettaClose == false)
.OrderBy(d => d.ApplicationDate).ThenBy(c => c.Category.CatName) ///Results should be =1 could be many
from offers in _context.Set<QuettaOffer>().Where(id => id.QuettaOfferId == req.Id)
.OrderBy(p => p.OfferPrice) //Results Should be 0 could be many
select new { req,offers,};
//Results are Empty When it should be ==1 req , 0 offers
var combinVm = vmq.Select(t => new ReqOferQestionAnswerVm //ReqOferQestionAnswerVm==Just parameters from 2 tables
{
ReqText = t.req.ReqText,
//Pram..
}).ToList();
return View(combinVm);
我不明白如果 2 table 有 0 个结果洞查询是 0 怎么办。
我的 类 长得像
public class QuettaReq
{
public QuettaReq()
{
QuettaOffer = new List<QuettaOffer>();
}
[Key] public int Id { get; set; }
//Pram
public virtual List<QuettaOffer> QuettaOffer { get; set; }
}
public class QuettaOffer
{
public QuettaOffer()
{
}
[Key] public int QuettaOfferId { get; set; }
public virtual QuettaReq QuettaReq { get; set; }
public int QuettaReqId { get; set; }
}
事实上你做了 Inner Join
而不是 Left Join
。
改成这样:
var vmq =
from req in _context.Set<QuettaReq>()
.Where(sd => sd.SiteUserId == _userManager.GetUserId(User) && sd.QuettaClose == false)
.OrderBy(d => d.ApplicationDate).ThenBy(c => c.Category.CatName) ///Results should be =1 could be many
join offers in _context.Set<QuettaOffer>().OrderBy(p => p.OfferPrice)
on req.Id equals offers.QuettaReqId into Joff
from offers in Joff.DefaultIfEmpty()
select new { req,offers,};
我试图从 2 个相关的 table 中查询数据,但是(EfCore 3.1 中没有 groupJoin)结果为空,因为相关的 table 没有项目。 我正在使用 mvc 核心 3.1 和 EF 核心 3。 代码
var vmq =
from req in _context.Set<QuettaReq>()
.Where(sd => sd.SiteUserId == _userManager.GetUserId(User) && sd.QuettaClose == false)
.OrderBy(d => d.ApplicationDate).ThenBy(c => c.Category.CatName) ///Results should be =1 could be many
from offers in _context.Set<QuettaOffer>().Where(id => id.QuettaOfferId == req.Id)
.OrderBy(p => p.OfferPrice) //Results Should be 0 could be many
select new { req,offers,};
//Results are Empty When it should be ==1 req , 0 offers
var combinVm = vmq.Select(t => new ReqOferQestionAnswerVm //ReqOferQestionAnswerVm==Just parameters from 2 tables
{
ReqText = t.req.ReqText,
//Pram..
}).ToList();
return View(combinVm);
我不明白如果 2 table 有 0 个结果洞查询是 0 怎么办。
我的 类 长得像
public class QuettaReq
{
public QuettaReq()
{
QuettaOffer = new List<QuettaOffer>();
}
[Key] public int Id { get; set; }
//Pram
public virtual List<QuettaOffer> QuettaOffer { get; set; }
}
public class QuettaOffer
{
public QuettaOffer()
{
}
[Key] public int QuettaOfferId { get; set; }
public virtual QuettaReq QuettaReq { get; set; }
public int QuettaReqId { get; set; }
}
事实上你做了 Inner Join
而不是 Left Join
。
改成这样:
var vmq =
from req in _context.Set<QuettaReq>()
.Where(sd => sd.SiteUserId == _userManager.GetUserId(User) && sd.QuettaClose == false)
.OrderBy(d => d.ApplicationDate).ThenBy(c => c.Category.CatName) ///Results should be =1 could be many
join offers in _context.Set<QuettaOffer>().OrderBy(p => p.OfferPrice)
on req.Id equals offers.QuettaReqId into Joff
from offers in Joff.DefaultIfEmpty()
select new { req,offers,};