简体   繁体   中英

how to get all the items of list c#

In a list i have 4 rows and I am try to get all the rows of the list but it is giving only one row, how to get all the rows of the list.

I have tried below code

public async Task<ResponseUserModel> get()
    {
        List<ResponseUserModel> responseUsers = new List<ResponseUserModel>();
        using (nae2sasqld0003Entities context = new nae2sasqld0003Entities())
        {
            var listt =  context.Producers.Select(all => all).ToList();

            foreach (var item in listt)
            {
                responseUsers.Add(new ResponseUserModel
                {
                    ProducerName = item.ProducerName,
                    ResidentState = item.ResidentState,
                    ResidentCity = item.ResidentCity,
                    ProducerStatus = item.ProducerStatus,
                    ProducerCode = item.ProducerCode,
                    MasterCode = item.MasterCode,
                    NationalCode = item.NationalCode,
                    LegacyChubbCodes = item.LegacyChubbCodes,
                    LegacyPMSCode = item.LegacyPMSCode,
                    ProducingBranchCode = item.ProducingBranchCode,
                    CategoryCode = item.CategoryCode
                });
            }
            return responseUsers;
        }
    }

please let me know where i to fix the issue

Use list to return all:

List<ResponseUserModel> responseUsers = new List<ResponseUserModel>();  

then

foreach (var item in listt)
{
       responseUsers.Add(new ResponseUserModel
       {
           ProducerName = item.ProducerName,
           ResidentState = item.ResidentState,
           ResidentCity = item.ResidentCity,
           ProducerStatus = item.ProducerStatus,
           ProducerCode = item.ProducerCode,
           MasterCode = item.MasterCode,
           NationalCode = item.NationalCode,
           LegacyChubbCodes = item.LegacyChubbCodes,
           LegacyPMSCode = item.LegacyPMSCode,                  
           ProducingBranchCode = item.ProducingBranchCode,
           CategoryCode = item.CategoryCode
       });
}
return responseUsers;

Note : change return type of the method to IList<ResponseUserModel>

or in this way

using (var context = new nae2sasqld0003Entities())
{
    return context.Producers.Select(item => 
        new ResponseUserModel
           {
               ProducerName = item.ProducerName,
               ResidentState = item.ResidentState,
               ResidentCity = item.ResidentCity,
               ProducerStatus = item.ProducerStatus,
               ProducerCode = item.ProducerCode,
               MasterCode = item.MasterCode,
               NationalCode = item.NationalCode,
               LegacyChubbCodes = item.LegacyChubbCodes,
               LegacyPMSCode = item.LegacyPMSCode,                  
               ProducingBranchCode = item.ProducingBranchCode,
               CategoryCode = item.CategoryCode
           }).ToList();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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