Zapnologica
Zapnologica

Reputation: 22556

Simple SELECT WHERE LINQ Query to a list

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();                  
        }
    }

Upvotes: 5

Views: 46638

Answers (2)

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

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>

Upvotes: 16

Kamil Budziewski
Kamil Budziewski

Reputation: 23087

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

Upvotes: 2

Related Questions