闲聊

用.NET EF Core对Mysql进行INNER JOIN多表查询

之前在网上找了好多资料,也没有比较好的回答

我要写这样一个sql语句:

SELECT
*
FROM
a
JOIN b ON a.PID = b.ID
JOIN c ON a.P2ID = c.ID
JOIN d ON c.PID = d.ID
AND c.P2ID = d.ID2

在.NET EF Core中我是这样做的:

IQueryable<a> cartIQ = from s in _dBContext.a select s;
cartIQ = cartIQ.Where(c => c.ID == param);
var cartList = cartIQ.Join(_dBContext.b, prodCart => prodCart.ProdId, prod => prod.Id, (prodCart, prod) => new { c = prodCart, b = prod })
.Join(_dBContext.SkuInfo, prod => prod.c.SkuId, sku => sku.SkuId, (prod, sku) => new { prod.c, prod.b, SkuInfo = sku })
.Join(_dBContext.SkuColorPic, t => new { t.SkuInfo.ProdId, t.SkuInfo.ColorId }, cpic => new { cpic.ProdId, cpic.ColorId }, (t, cpic) => new { t.c, t.b, t.SkuInfo, SkuColorPic = cpic });