error on execute sql on entity framework core ?

Ahmed Salah Abed Elaziz 390 Reputation points
2023-03-14T10:05:16.4+00:00

i work on .net core 7 i get error error CS0029: Cannot implicitly convert type 'int' to 'System.Linq.IQueryable<UC.AppRepository.Core.Entities.ApplicationsData>'

my statement as below :

 
 public List<ApplicationDto> ListOfApplications(ApplicationsFilterDto emp)
        {
            
           var appList  = _context.Database.ExecuteSql($"select ApplicationsData.ApplicationID as ApplicationId,Application_Name as ApplicationName,CommonName,d.DetailsName as TypeOfApplication,AccessType,d.DetailsName Criticality,o.OwnerName as ApplicationOwner,DRRequired,se.[DB_Name] as [DataBase] from ApplicationsData left join [dbo].[Details] d with(nolock) on d.ID=ApplicationsData.ApplicationType and d.HeaderId=6 left join [dbo].[Details] d2 with(nolock) on d2.ID=ApplicationsData.Criticality and d2.HeaderId=7\r\nleft join dbo.[Owner] o with(nolock) on o.ApplicationId=ApplicationsData.ApplicationID\r\nleft join dbo.[DataBase] se with(nolock) on se.ServerID=ApplicationsData.ServerId where ApplicationsData.ServerId={emp.serverID} OR SE.[DBID]={emp.databaseId} OR o.OwnerId={emp.ownerId}"); 
          
            return appList;  
         }

so how to solve issue please ?

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-03-14T13:36:31.4033333+00:00

    Hi @Ahmed Salah Abed Elaziz,

    The return type for ExecuteSql should be int. You should use FromSql, then the issue could be fixed.

            public int cmd2()
            {
                var appList = _context.Database.ExecuteSql($"select *from [HandyMansToolDb].dbo.Table2  ");
    
                return appList;
    
            }
            public IActionResult cmd3()
            {
                
                var appList = _context.Table2Users.FromSql($"select *from [HandyMansToolDb].dbo.Table2 ");
    
                return Ok(appList);
    
            }
            public IActionResult cmd4()
            {
    
                var appList = _context.Table2Users.FromSqlRaw($"select *from [HandyMansToolDb].dbo.Table2 ");
    
                return Ok(appList);
    
            }
    

    enter image description here


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Jason Pan

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.