Simple SELECT WHERE LINQ Query to a list

c# entity-framework linq

Question

I am new to LINQ,

I want to generate a list of objects from my dbcontext where a certain field is set to true.

This is what I have so far, but I am getting an error about a select?

using (var db = new dbContext())
{
    return (from s in db.sims.Where(x=>x.has_been_modified == true) select x).ToList();               
}

EDIT:

    //Returns a list of entries which where marked as edited in the sim managment database
    private List<String> GetUpdatedEntries()
    {
        using (var db = new dbContext())
        {
            return db.sims.Where(x => x.has_been_modified).ToList();                  
        }
    }
1
4
7/16/2015 2:04:32 PM

Accepted Answer

select s, not x and this will work. (because you do from s)

shorter way

return db.sims.Where(x => x.has_been_modified).ToList();

For your Edit

the method return type should be a List<Sim>, not a List<String>

16
1/14/2014 7:41:49 AM

Popular Answer

This will work

return db.sims.Where(x=>x.has_been_modified).ToList();
  1. method Linq looks cleaner here
  2. you don't need to check your bool against true
  3. in your previous answer you used s as context and selected x, changing to select s should work also
  4. Consider using lazy loading and don't add ToList at the end of every query


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow