Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Converting from a Linq Query to a List of Type

26.06.2018 (👁4147)

 

Task: Converting from a Linq Query to a List of Type

 

Description:

I want to convert a result of a Linq query into asp.net core into an output list of a certain type.

Unfortunately, the output is generated with an error

 

Erroneous code

//*gets last 10 Projects with View_Sum

var query = (from n in _dbContext.tbl_Projects

             where n.IsDraft == false

             orderby n.IDProject descending

             select new { n }).Take(50);

//--</ Get Linq.Query >--

 

 

//----< fill Data_to_View >----

//*error

List<ProjectModel> dataList = query.ToList<ProjectModel>();

 

 

 

'IQueryable' anonymous type: ProjectModel n '' Does not contain a definition for 'ToList' and the best extension method overload 'AsyncEnumerable.ToList <ProjectModel> (IAsyncEnumerable <ProjectModel>)' requires a receiver of type 'IAsyncEnumerable <ProjectModel > '

 

Severity

Code

Description

Project

Line

Suppression State

Error

CS1929

'IQueryable<<anonymous type: ProjectModel n>>' does not contain a definition for 'ToList' and the best extension method overload 'AsyncEnumerable.ToList<ProjectModel>(IAsyncEnumerable<ProjectModel>)' requires a receiver of type 'IAsyncEnumerable<ProjectModel>'

Freelance

46

N/A

 

 

Solution:

You can not create the submissions with new {n}

// GET: /api/index

public List<ProjectModel> Index()

{

    ///-------------< Index >------------- 

 

    //--< Get Linq.Query >--

    //*gets last 10 Projects with View_Sum

    var query = (from n in _dbContext.tbl_Projects

                 where n.IsDraft == false

                 orderby n.IDProject descending

                 select  n ).Take(50);

    //--</ Get Linq.Query >--

 

 

    //----< fill Data_to_View >----

    List<ProjectModel> dataList = query.ToList<ProjectModel>();

    

 

    //< out >

    //*output to client

    return dataList;

    //</ out >

    ///-------------</ Index >------------- 

}

 

 

 

In Asp.Net Core project