Share via

Entity Framework Core SELECT from EF3

Cloud Developer 21 Reputation points
2022-11-22T15:33:23.843+00:00

I have an EF 3 LINQ Query that I'm trying to translate to EF CORE and it's not working. Can anyone help and point me to a good EF CORE documentation? We've upgraded a .NET 3 WebAPI EF 3 to .NET 6 using EF CORE and it's becoming a nightmare.

My current query/method looks like this:

[HttpGet("{source}/{type}"]  
public async Task<ActionResults> GetCars(string source, string type)  
{  
      var t = _db.GetCars;  
      
      return Json(new  
       {  
            data = await t  
            .Select(x=> new   
             {  
                  make = x.make,  
                  model = x.model,  
                  year = x.year,  
                  type = x.type,  
                  source = x.source  
             }).Where(x=> x.source == source && x.type == type).ToListAsync()  
  
       });  
}  

how can I get this working using EF CORE? If I copy this into my .NET 6 EF CORE WebAPI it fails on compiling. Type and Source are passed into the method (type = new, used, ordered, source = walkin, web site, phone call)

Developer technologies | .NET | Entity Framework Core
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Developer technologies | ASP.NET Core | Other

2 answers

Sort by: Most helpful
  1. AgaveJoe 31,361 Reputation points
    2022-11-22T17:19:17.833+00:00

    I'm still not sure what the error is and I'm not a fan of returning anonymous types but the following is a typical Web API pattern.

    [HttpGet("/api/Employee/GetEmployeeByName/{name}/{surname}")]  
    public async Task<ActionResult> GetEmployeeByName(string name, string surname)  
    {  
        var result = await _context.Employees  
            .Where(e => e.Name == name & e.Surname == surname)  
            .Select(e => new { id = e.EmployeeId, deptId = e.DepartmentId }).ToListAsync();  
      
        if (result == null | result?.Count() == 0)  
        {  
            return NotFound();  
        }  
      
        return Ok(result);  
    }  
    

    The results

    [  
      {  
        "id": 1,  
        "deptId": 1  
      }  
    ]  
    

    If the response is empty perhaps it is a case issue?

    .Where(e => e.Name.ToLower() == name.ToLower() & e.Surname.ToLower() == surname.ToLower())  
    

    Was this answer helpful?

    0 comments No comments

  2. Cloud Developer 21 Reputation points
    2022-11-22T17:15:45.27+00:00

    I figured it out;

    public async Task<ActionResults> GetCars(string source, string type)  
     {  
           var t = _db.GetCars  
    	.Where(x=> x.source == source && x.type == type)  
            .Select(c => new   
             {  
                 make = c.make,  
                 model = c.model,  
                 year =c.year         
             });  
      
            return Ok(await t.ToListAsync());  
            
     }  
    

    Was this answer helpful?

    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.