Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

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

$
0
0

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

publicpartialclassNPSEntity{publicint ID { get;set;}publicDateTime?NPSDate{ get;set;}publicdecimal?NPSAmount{ get;set;}publicDateTime?AddDate{ get;set;}publicDateTime?ModDate{ get;set;}[StringLength(20)]publicstringUserID{ get;set;}[StringLength(2)]publicstringCountryCode{ get;set;}}publicList<NPSEntity>NPSData{ get;privateset;}

Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>