C# EF Core MySQL 到 LINQ 不返回任何内容

问题描述 投票:0回答:1
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,仍然没有返回任何内容。

c# entity-framework linq
1个回答
0
投票

您可以尝试更改包含方法作为导航属性

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();
© www.soinside.com 2019 - 2024. All rights reserved.