Querying using LINQ and the Entity Framework

When using the Entity Framework and if you want to query using LINQ, this is how it is done:

Make sure to have your Context set up. My context is ListofRestaurantsContext, and my table name is List_Of_Restaurants
context.TableName.SqlQuery

Raw SQL Query: (Dangerous)


                using (var context = new ListofRestaurantsContext())
                {
                    var blogs = context.List_Of_Restaurants.SqlQuery("Select * from List_Of_Restaurants").ToList();
                    foreach (var test1 in blogs)
                    {
                        var assasa = test1.Address;
                    }
                    var test3 = "";

                }

Using simple “where”:

context.TableName.Where(n => n.Name == “Blah”);

  using (var context = new ListofRestaurantsContext())
                {
                    var blogs = context.List_Of_Restaurants.Where(n => n.restaurant_id == 1).ToList();
                    foreach (var test1 in blogs)
                    {
                        var assasa = test1.Address;
                    }
                    var test3 = "";

                }

Leave a Reply