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

Solved: Cannot implicitly convert type System.Linq.IQueryable To IEnumerable

14.07.2018 (👁12963)

 

Error message:

Cannot implicitly convert type 'System.Linq.IQueryable<long>' to 'System.Collections.Generic.IEnumerable<string>

 

Error code:

Severity

Code

Description

Error

CS0266

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

 

Bad code:

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

//*gets last 10 Angebote with View_Sum

var query = (from n in _dbContext.tbl_Angebote

             where n.IDOwner == IDCurrent_User

             orderby n.IDAngebot descending

             select n.IDAngebot );

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

 

 

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

IEnumerable<string> dataList = query;

  

//< out >

//*output to client

return dataList;

//</ out >

 

 

In Visual Studio

 

 

 

Solution:

 

You can explicitly convert the Linq query with the AS naming

Example:

IEnumerable<string> dataList = query as IEnumerable<String>;

 

Corrected code example

// GET: /api/index

public async Task<IEnumerable<string>> Index()

{

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

    //< get UserClaim Info >

    //*get User from Token

    var userClaim_in_Token = HttpContext.User.Claims.Where(c => c.Type == ClaimsIdentity.DefaultNameClaimType).FirstOrDefault(); //User as Name

    if(userClaim_in_Token==null)

    {

        return null;

    }

    string sEmail = userClaim_in_Token.Value;

    //</ get UserClaim Info >

 

    //< check user >

    long IDCurrent_User = await Get_UserID(sEmail);

    if (IDCurrent_User == 0) { return null; }

    //</ check user >

 

 

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

    //*gets last 10 Angebote with View_Sum

    var query = (from n in _dbContext.tbl_Angebote

                 where n.IDOwner == IDCurrent_User

                 orderby n.IDAngebot descending

                 select n.IDAngebot );

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

 

 

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

    IEnumerable<string> dataList = query as IEnumerable<String>;

    

 

    //< out >

    //*output to client

    return dataList;

    //</ out >

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

}

 

 

#: Asp.Net Core MVC, Linq Api Webapi Controller