LINQ: Need help in return format

Jerry Lipan 916 Reputation points
2022-03-21T00:28:05.523+00:00

Hi,

I've table as following,

184927-21032022-001.png

Model: ApplicationDbContext

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Threading.Tasks;  
using Microsoft.EntityFrameworkCore;  
  
namespace SalesSystem.Models  
{  
    public class ApplicationDbContext : DbContext  
    {  
          
        public ApplicationDbContext(DbContextOptions options)  
         : base(options)  
        { }  
  
        protected override void OnModelCreating(ModelBuilder modelBuilder)  
        {            
            modelBuilder.Entity<AspNetUsers>(entity =>  
            {  
                entity.Property(e => e.Email)  
                .HasMaxLength(256)  
                .IsUnicode(false)  
                ;  
  
                entity.Property(e => e.Name)  
               .HasMaxLength(256)  
               .IsUnicode(false)  
               ;  
  
            });  
  
              
  
        }  
          
        public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }  
          
  
    }  
}  
  

Controller: NoticeController

using Microsoft.AspNetCore.Mvc;  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Threading.Tasks;  
using SalesSystem.Models;  
  
namespace SalesSystem.Controllers  
{  
    public class NoticeController : Controller  
    {  
        private ApplicationDbContext _context;  
  
        public NoticeController(ApplicationDbContext context)  
        {  
            this._context = context;              
        }  
  
        public IActionResult Index()  
        {  
            
  
            return View();  
        }  
          
        public IActionResult AddNotice()  
        {  
            List<AspNetUsers> _AspNetUsers = _context.AspNetUsers.ToList();  
            var GetSalesPersonList = _AspNetUsers  
                                     .Select(d => new { d.Id, d.Name }).ToList();  
                                      
  
            return View();  
        }  
    }  
  
}  
  

I got this,

184905-21032022-002.png

You can see, it return in format: Id & Name

My question is, how to return in format: SalesPersonId & SalesPersonDisplay ?

Id = SalesPersonId

Name = SalesPersonDisplay

Please help

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,237 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,036 Reputation points Microsoft Vendor
    2022-03-21T01:34:27.93+00:00

    Hi @Jerry Lipan ,

    My question is, how to return in format: SalesPersonId & SalesPersonDisplay ?

    Id = SalesPersonId

    Name = SalesPersonDisplay

        var GetSalesPersonList = _AspNetUsers    
    
                                       .Select(d => new {  d.Id, d.Name }).ToList();  
    

    You can modify the above code as below:

     var GetSalesPersonList = _AspNetUsers  
                                       .Select(d => new { SalesPersonId = d.Id, SalesPersonDisplay = d.Name }).ToList();  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

0 additional answers

Sort by: Most helpful