序列在查询中不包含任何元素

时间:2017-06-11 16:04:19

标签: c# entity-framework entity-framework-core

查询:

var result = await this.Context.ShopProducts
    .Include(prd => prd.Category)
        .ThenInclude(cat => cat.Culture)
    .Include(prd => prd.InfoItems)
    .SingleOrDefaultAsync(prd => prd.Id.Equals(id) && prd.CategoryId.Equals(culture));
  

编辑:更新了实体和查询以反映新设计并添加了SQL查询

实体:

产品:

[Table("ShopProduct")]
public class Product : ShopBase
{
    public bool Active { get; set; } = true;
    public int CategoryId { get; set; }
    public virtual Category Category { get; set; }

    public ICollection<ProductInfo> InfoItems { get; set; } = new HashSet<ProductInfo>();
}

ProductInfo:

[Table("ShopProductInfo")]
public class ProductInfo : ShopBase
{
    public int ProductId { get; set; }
    public int CultureId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Sum { get; set; }        

    public ICollection<GraphicItem> GraphicItems { get; set; }
}

我想要的是只选择具有CultureId的ProductInfo对象,该对象等于Category CultureId属性。选择时,我提供产品ID和类别ID。

我想复制像这样的SQL查询:

DECLARE @prdId  INT,
        @catId  INT

SET @prdId = 1
SET @catId = 1

SELECT  prd.*, 
        info.*, 
        cat.* 
FROM    ShopProduct prd, 
        ShopProductInfo info, 
        ShopCategory cat 
WHERE   prd.Id = @prdId
AND     prd.CategoryId = cat.Id
AND     cat.Id = @catId
AND     cat.CultureId = info.CultureId

1 个答案:

答案 0 :(得分:-1)

此错误意味着,linq查询返回一些值,否则会出现异常错误