简体   繁体   English

在查询中使用 Select() 而不是 Include() 时出错

[英]Error when using Select() instead of Include() in a query

I have the following query:我有以下查询:

var catInclude = _db.Cat
    .Where(x => x.ProvId == request.ProvId)
    .Include(x => x.CatItems)
    .SingleOrDefault(p => p.Id == request.ProvId
         cancellationToken: cancellationToken);

As I don't want to get all properties from CatItems with Include(), I have created the following query:由于我不想使用 Include() 从 CatItems 中获取所有属性,因此我创建了以下查询:

var catSelect = _db.Cat
.Where(x => x.ProvId == request.ProvId)
.Select(p ==> new
    { Provider = p,
      Items = p.CatItems.Select(x => new List<CatItems> { new CatItems
{ Id = x.Id, Name = x.Name, Price = x.Price } }
})})
SingleOrDefault(cancellationToken: cancellationToken);

But something is wrong in the 2nd query because here return _mapper.ProjectTo<CatDto>(cat) I get the following error:但是第二个查询出了点问题,因为这里return _mapper.ProjectTo<CatDto>(cat)我收到以下错误:

Argument 1: cannot convert from '<anonymous type: Db.Entities.Cat Prov, System.Colletions.Generic.IEnumerable<System.Colletions.Generic.List<Models.CatItems> > Items>' to 'System.Linq.IQueryable'参数 1:无法从 '<anonymous type: Db.Entities.Cat Prov, System.Colletions.Generic.IEnumerable<System.Colletions.Generic.List<Models.CatItems> > Items>' 转换为 'System.Linq.IQueryable'

Here is my CatDto :这是我的CatDto

public class CatDto
{
   public int ProvId { get; set; }
   public List<CatItems> CatItems { get; set; }
}

Here are my entities:这是我的实体:

public class Prov
{
   public int Id { get; set; }
   public Cat Cat { get; set; }
}

public class Cat
{
   public int Id { get; set; }
   public int ProvId { get; set; }
   public List<CatItems> CatItems { get; set; }
}

public class CatItems
{
   public int Id { get; set; }
   public int CatId { get; set; }
}

Is there a way to recreate the 2nd query and use it?有没有办法重新创建第二个查询并使用它?

I mean, even the exception is pretty self-explanatory.我的意思是,即使是例外也是不言自明的。 Nevertheless:尽管如此:

You are performing a .Select(...) .您正在执行.Select(...) It returns an Anonymous type.它返回一个匿名类型。 So, your catSelect is an anonymous type, thus the AutoMapper fails.因此,您的catSelect是匿名类型,因此AutoMapper失败。

The quickest fix is to just cast (Cat)catSelect before mapping.最快的解决方法是在映射之前只投射(Cat)catSelect
Or, you can dig deeper into how does AutoMapper play with anonymous types.或者,您可以深入了解 AutoMapper 如何处理匿名类型。

Main difference that instead of returning List of CatItems , your code returns IEnumerable<List<CatItems>> for property Items .主要区别在于,您的代码不是返回List of CatItems ,而是为属性Items返回IEnumerable<List<CatItems>>

So, just correct your query to project to List :因此,只需更正您的查询以投射到List

var catSelect = await _db.Cat
    .Where(x => x.ProvId == request.ProvId)
    .Select(p => new
    { 
        Provider = p,
        Items = p.CatItems.Select(x => new CatItems
        { 
            Id = x.Id, 
            Name = x.Name, 
            Price = x.Price 
        })
        .ToList()
    })
    .SingleOrDefaultAsync(cancellationToken: cancellationToken);

I feel like you can make most of the classes inherent Id and why is public cat CAT {get;我觉得你可以让大多数类固有 ID,为什么 public cat CAT {get; set;} i thought you were supposed to initialize some kind of value set;} 我以为你应该初始化某种值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM