locked
C# LINQ: How to select multiple field and return list RRS feed

  • Question

  • User-1223304583 posted

    I have to select multiple fields and need to return a list.

    So I try to do it this way but I am getting an error. The error message is:

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

    Here is the code which I have tried and got the error:

    private List<NPSEntity> GetData()
    {
        CountryCount = 0;
        using (var db = new NPSDbContext())
        {
        NPSData = (from n in db.NPSDatas
               orderby n.AddDate, n.CountryCode
               where DbFunctions.TruncateTime(n.NPSDate) >= StartDate.Date && DbFunctions.TruncateTime(n.NPSDate) <= EndDate
               select new
                   {
                       NPSDate = DbFunctions.TruncateTime(n.NPSDate),
                       NPSAmount = n.NPSAmount,
                       AddDate = n.AddDate,
                       ModDate = n.ModDate,
                       UserID = n.UserID,
                       CountryCode = n.CountryCode
                   }
                   );
    
    
        }
    
        if (NPSData!=null)
        {
        CountryCodes = NPSData.Select(e => e.CountryCode).Distinct();
        CountryCount = CountryCodes.Count();
        }
        return NPSData;
    }
    select new

    is throwing the error that I pasted above.

    What do I need to change in my code?

    this is my NPSEntity class code

    public partial class NPSEntity
    {
        public int ID { get; set; }
    
        public DateTime? NPSDate { get; set; }
    
        public decimal? NPSAmount { get; set; }
    
        public DateTime? AddDate { get; set; }
    
        public DateTime? ModDate { get; set; }
    
        [StringLength(20)]
        public string UserID { get; set; }
    
        [StringLength(2)]
        public string CountryCode { get; set; }
    }
    
    public List<NPSEntity> NPSData { get; private set; }
    Monday, February 13, 2017 12:49 PM

All replies

  • User2117486576 posted

    Calling ToList() on the IQueryable should do it:

    CountryCodes = NPSData.Select(e => e.CountryCode).Distinct().ToList();

    Monday, February 13, 2017 12:58 PM
  • User1930269528 posted

    As you are fetching data using Linq from the database, so the type of the variable would be IQueryable<>, and you can materialize the reuslt in to a List<> by forcing the query to be executed and results to be brought in the memory by calling ToList() method.

    So just change the last line from :

    return NPSData;

    to:

    return NPSData.ToList();

    Monday, February 13, 2017 1:01 PM
  • User-1223304583 posted
    Can u rectify and post full rectified code?
    Monday, February 13, 2017 6:42 PM
  • User-271186128 posted

    Hi mamoni.kol2017,

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

    Please modify your code as below:

    private List<NPSEntity> GetData()
    {
        CountryCount = 0;
        using (var db = new NPSDbContext())
        {
        NPSData = (from n in db.NPSDatas
                   orderby n.AddDate, n.CountryCode
                   where DbFunctions.TruncateTime(n.NPSDate) >= StartDate.Date && DbFunctions.TruncateTime(n.NPSDate) <= EndDate
                   select new NPSEntity
                   {
                       NPSDate = DbFunctions.TruncateTime(n.NPSDate),
                       NPSAmount = n.NPSAmount,
                       AddDate = n.AddDate,
                       ModDate = n.ModDate,
                       UserID = n.UserID,
                       CountryCode = n.CountryCode
                   }
                   ).ToList();
    
    
        }
    
        if (NPSData!=null)
        {
        CountryCodes = NPSData.Select(e => e.CountryCode).Distinct();
        CountryCount = CountryCodes.Count();
        }
        return NPSData;
    }

    Best regards,
    Dillion

    Tuesday, February 14, 2017 8:20 AM