var IdSubProduct = new MySqlConnector.MySqlParameter("IdSubProduct", this.parameter.Id.ToString());
FormattableString cmd = $@"SELECT spr.*, c.Name CustomerName FROM ProductSubProductRate spr
LEFT JOIN LibraryCustomer c ON (c.Id = spr.CustomerId)
WHERE (spr.IdSubProduct = {IdSubProduct})";
this.contextSQL.ProductSubProductRate.FromSql(cmd).ToList();
this.contextSQL.ProductSubProductRate
.Include("Customers")
.Where(pr => pr.IdSubProduct == this.parameter.Id.ToString())
.Select(pr => new
{
pr.Id,
pr.EffectiveDate,
pr.Rate,
pr.Additional,
pr.Creator,
pr.Modifier,
CustomerName = pr.Customers.Name
})
.OrderByDescending(pr => pr.EffectiveDate).ToList();
这不一样吗?为什么 SQL 返回的正是我需要的结果,而 LINQ 什么也不返回。表关系按照here所写完成。即使我删除了 .include,仍然没有返回任何内容。
您可以尝试更改包含方法作为导航属性
this.contextSQL.ProductSubProductRate
.Include(pr=>pr.Customers)
.Where(pr => pr.IdSubProduct == this.parameter.Id.ToString())
.Select(pr => new
{
pr.Id,
pr.EffectiveDate,
pr.Rate,
pr.Additional,
pr.Creator,
pr.Modifier,
CustomerName = pr.Customers.Name
})
.OrderByDescending(pr => pr.EffectiveDate).ToList();