简体   繁体   English

Asp.Net 核心/实体框架核心 - System.Linq.Enumerable+SelectEnumerableIterator`2[MyClass,System.String]

[英]Asp.Net Core / Entity Framework Core - System.Linq.Enumerable+SelectEnumerableIterator`2[MyClass,System.String]

I am trying to return a json array of my 3rd level depth related data, the issue here is that I get the result with the right property name but with a non clear value content, I failed to find a similar case to solve it.我正在尝试返回我的 3 级深度相关数据的 json 数组,这里的问题是我得到的结果具有正确的属性名称但值内容不明确,我未能找到类似的案例来解决它。 From the returned value message it looks like I am returning a queryable instead of the final result and I need to iterate over it, I've tried several ways to achive that but failed to find the right one.从返回的值消息看来,我正在返回一个可查询的而不是最终结果,并且我需要对其进行迭代,我尝试了几种方法来实现它,但未能找到正确的方法。

The json result: json 结果:

[
{
"registeredYear": "System.Linq.Enumerable+SelectEnumerableIterator`2[MyPath.Groups.GroupYear,System.String]"
}
]

The api endpoint api 端点

public async Task<ActionResult<IEnumerable<UserGroup>>> GetUserGroupYears(string email, string groupName)
    {
        var groupYears = await _repo.GetUserGroupYears(email, groupName);
        var mappedEntities = _mapper.Map<GroupYearsListDto[]>(groupYears);
        return Ok(mappedEntities);
    }

The Repository method存储库方法

public async Task<IEnumerable<UserGroup>> GetUserGroupYears(string email, string groupName)
{
    var userGroupYears = _context.UserGroups
                          .Include(uo => uo.Group.GroupYears)
                            .ThenInclude( oy => oy.Year)
                          .Where(uo => uo.Group.Name == groupName && uo.Email == email );
            return await userGoupYears.ToArrayAsync();
}

The used classes:使用的类:

public class UserGroup
{
    public string Email { get; set; }
    
    public string UserId { get; set; }
    public virtual User User { get; set; }
    
    public string GroupId { get; set; }
    public virtual Group Group { get; set; }
}

public class Group
{
    public string Name { get; set; }
    public virtual ICollection<UserGroup> Users { get; set; }
    public virtual ICollection<GroupYear> GroupYears { get; }
}

public class GroupYear    {
    public string GroupId { get; set; }
    public virtual Group Group { get; set; }

    public string YearId { get; set; }
    public virtual Year Year { get; set; }
    public string RegisteredYear { get; set; }
}

The data transfer object and the mapping:数据传输 object 和映射:

public class GroupYearsListDto
{
    public string RegisteredYear { get; set; }
}
public CoreMappingProfiles()
{
    CreateMap<UserGroup, GroupYearsListDto>()
      .ForMember(
          dest => dest.RegisteredYear,
          opt => opt.MapFrom(src => src.Group.GroupYears.Select(x => x.RegisteredYear))
          );                  
}

Update : Attaching a debugger shows that the repository method is returning an IQueryable including the correct values and the controller method makes something wrong when mapping.更新:附加调试器显示存储库方法正在返回一个包含正确值的 IQueryable,并且 controller 方法在映射时出错。 So I think the following line is responsible of that wrong result:所以我认为以下行是导致错误结果的原因:

var mappedEntities = _mapper.Map<GroupYearsListDto[]>(GroupYears);

You are getting this JSON result:你得到这个 JSON 结果:

[
  {
    "registeredYear": "System.Linq.Enumerable+SelectEnumerableIterator`2[MyPath.Groups.GroupYear,System.String]"
  }
]

Because you are mapping an IEnumerable<string> to a string , as I mentioned in my comment.因为您正在将IEnumerable<string>映射到string ,正如我在评论中提到的那样。 So essentially you are getting the same as:所以基本上你得到的是相同的:

CreateMap<UserGroup, GroupYearsListDto>()
    .ForMember(
        dest => dest.RegisteredYear,
        opt => opt.MapFrom(src =>
        {
            IEnumerable<string> registeredYears = src.Group.GroupYears.Select(x => x.RegisteredYear);
            return registeredYears.ToString();
        })
    );

And registeredYears.ToString() is "System.Linq.Enumerable+SelectEnumerableIterator`2[MyPath.Groups.GroupYear,System.String]" .registeredYears.ToString()"System.Linq.Enumerable+SelectEnumerableIterator`2[MyPath.Groups.GroupYear,System.String]"

I imagine you will either:我想你会:

  1. Only have one - so do something like: src.Group.GroupYears.Select(x => x.RegisteredYear).Single()只有一个 - 所以做类似的事情: src.Group.GroupYears.Select(x => x.RegisteredYear).Single()
  2. Have multiples - so do something like: string.Join(", ", src.Group.GroupYears.Select(x => x.RegisteredYear))有多个 - 所以做类似的事情: string.Join(", ", src.Group.GroupYears.Select(x => x.RegisteredYear))

You have many options, but you need to actually return a string to that property or else you will just get the ToString() version of IEnumerable<string> .您有很多选择,但您实际上需要向该属性返回一个string ,否则您只会获得IEnumerable<string>ToString()版本。

UPDATE:更新:

Based on your comments below, you can try this:根据您在下面的评论,您可以尝试以下操作:

Repository:存储库:

public IQueryable<GroupYear> GetGroupYears(string email, string groupName)
{
    return _context
        .UserGroups
        .Where(x => x.Group.Name == groupName && x.Email == email)
        .SelectMany(x => x.Group.GroupYears);
}

Controller: Controller:

public async Task<ActionResult<GroupYearsListDto[]>> GetGroupYears(string email, string groupName)
{
    var groupYears = _repo.GetGroupYears(email, groupName);
    var projection = _mapper.ProjectTo<GroupYearsListDto>(groupYears)
    var mappedEntities = await projection.ToArrayAsync();
    return Ok(mappedEntities);
}

Profile:轮廓:

CreateMap<GroupYears, GroupYearsListDto>();

暂无
暂无

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

相关问题 ASP.NET实体框架LINQ to Entities无法识别方法&#39;System.DateTime ToDateTime(System.String) - ASP.NET Entity Framework LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String) 出现错误-&gt; System.Linq.Enumerable + WhereSelectListIterator`2 [Tdsb.Aris.Pims.Entity.PartnershipFunding,System.String] - Getting error -> System.Linq.Enumerable+WhereSelectListIterator`2[Tdsb.Aris.Pims.Entity.PartnershipFunding,System.String] LINQ foreach循环中的System.Linq.Enumerable + d__3a`1 [System.String]错误 - System.Linq.Enumerable+d__3a`1[System.String] error in LINQ foreach loop ASP.NET 核心 Web API 使用实体框架核心 - ASP.NET Core Web API using Entity Framework Core Asp.net核心实体框架Linq,其中查询测试两个布尔是否相等 - Asp.net Core Entity Framework Linq where query testing if two bools are equal 在 ASP.NET 核心 Web ZDB974238714CA8DE634A7CE1DZ08A 中避免与 Linq 的实体重复 - Avoid entity duplication with Linq in ASP.NET Core Web API ASP.NET 核心 Web API - System.InvalidOperationException: LINQ 表达式 'DbSet<mandate> ()</mandate> - ASP.NET Core Web API - System.InvalidOperationException: The LINQ expression 'DbSet<Mandate>() 当使用 LINQ GroupBy 时,ASP.NET 内核尝试将 object 转换为 System.Int64 - ASP.NET Core tries to cast an object to System.Int64 when using LINQ GroupBy ASP.NET C#System.Linq.Enumerable + WhereSelectEnumerable错误 - ASP.NET C# System.Linq.Enumerable+WhereSelectEnumerable Error ASP.NET Core Linq - ASP.NET Core Linq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM