Search code examples
c#entity-frameworklinqlinq-to-sqllinq-to-entities

C# LINQ - Prop string has value of System.Collections.Generic.List`1[System.String]


As it's described in title I'm getting somehow like list which contains oblivously list of strings?

How come?

Here is my query:

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);

Issue is that ResponsiblePerson value is System.Collections.Generic.List`1[System.String] instead of Full name..

What's wrong here?

Thanks


Solution

  • You are returning a List. You must use something similar to FirstOrDefaultAsync() or SingleOrDefaultAsync() in your case.

    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);