C# LINQ - Prop字符串的值为System.Collections.Generic.List`1[System.String]。

问题描述 投票:0回答:1

正如标题中所描述的那样,我得到了一些类似于list的东西,其中包含了遗忘的字符串列表?

为什么会这样?

这是我的疑问。

var productChangesUpdate = await _dbContext.ProductUpdates.Where(x => x.ProductId == ProductId)
                          .Select(x => new ProductChangeDto
                          {
                              Id = x.Id,
                              Title = x.Title,
                              Price = x.Price,
                              Date = x.CreatedDate,
                              ResponsiblePerson = x.ProductDeploy.ProductDeployResponsiblePersons.Select(x => x.User.FirstName + x.User.LastName).ToString()
                          }).ToListAsync(cancellationToken);

问题是... ResponsiblePerson 值是System.Collections.Generic.List`1[System.String],而不是Full name。

这里有什么问题?

谅谅

c# entity-framework linq linq-to-sql linq-to-entities
1个回答
1
投票

你返回的是一个 List. 你必须在你的情况下使用类似FirstOrDefaultAsync()和SingleOrDefaultAsync()的东西。

var productChangesUpdate = await _dbContext.ProductUpdates.Where(x => x.ProductId == ProductId)
                          .Select(x => new ProductChangeDto
                          {
                              Id = x.Id,
                              Title = x.Title,
                              Price = x.Price,
                              Date = x.CreatedDate,
                              ResponsiblePerson = x.ProductDeploy.ProductDeployResponsiblePersons.Select(x => x.User.FirstName + x.User.LastName).ToString()
                          }).FirstOrDefaultAsync(cancellationToken);
© www.soinside.com 2019 - 2024. All rights reserved.