Search code examples
.net-corerazor-pagesentity-framework-core-3.1

Entity Framework query to return list with simple filter


Is there a simple way to modify this code to return only records where LocationID matches the id I'm trying to pass as a parameter? Needless to say, this doesn't compile. I thought Entity Framework was meant to make things easier, but I've searched online and can't find an understandable example of how to assign a simple query where a field in a single table/entity matches a number.

public async Task<List<PC>> GetPCsAsync(int id)
{
   // Get our data. Don't yet know how to feed the variable to EF/Linq
   PCList = await (from p in db.PC 
   select new PC {p.LocationID = id}).ToListAsync();
   return PCList;
}

Thanks.


Solution

  • And also if you want to do it using Query Syntax it would be something like this:

    PCList = await (from p in db.PC 
                    where p.LocationID == id
                    select p).ToListAsync();
    

    Here's a link to understand the differences between Query and Method syntax.