Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();                  
        }
    }
like image 797
Zapnologica Avatar asked Jan 14 '14 07:01

Zapnologica


2 Answers

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>

like image 118
Raphaël Althaus Avatar answered Sep 23 '22 14:09

Raphaël Althaus


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
like image 43
Kamil Budziewski Avatar answered Sep 21 '22 14:09

Kamil Budziewski