Solved

linq to entities Method-Based Query Syntax

Posted on 2014-11-04
1
238 Views
Last Modified: 2016-02-15
I'm practicing writing LINQ to Entities queries.

I have a ASP.NET web forms application.
I created an Entity Data Model using database First approach for the Northwind Sql Server Database.

On my .aspx page I have a GridView and I'm binding that to my Employees entity.

I read the info on this link:
Queries in LINQ to Entities
http://msdn.microsoft.com/en-us/library/vstudio/bb399367%28v=vs.100%29.aspx

On that link it says, when using LINQ to Entities, there are 2 ways to write a query. One way is to use Query Expression syntax and the other is using Method-Based Query Syntax with lambda expressions.


Query Expression Syntax Example:

I created an example that looks like this and it works just fine.
This example uses Query Expression Syntax
My code looks like this:


EmployeeGrid.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EmployeeGrid.aspx.cs" Inherits="SampleApp.EmployeeGrid" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

Open in new window


EmployeeGrid.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// reference project
using SampleApp;
 
namespace SampleApp
{
    public partial class EmployeeGrid : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // using Linq to Entities Query Expression Syntax

            using (NorthwindEntities myEntities = new NorthwindEntities())
            {
                var authorizedEmployees = from employee in myEntities.Employees
                                          //where employee.EmployeeID == 1
                                          orderby employee.EmployeeID ascending                                                                                select new { employee.EmployeeID, employee.FirstName, employee.LastName, employee.Title };
                
                // We canot bind Iqueryable directly. We have to change into ToList()
                GridView1.DataSource = authorizedEmployees.ToList();
 
                GridView1.DataBind();
            }
 
        }
    }

Open in new window


Now I'm trying  to do the same example but re-write the linq query in EmployeeGrid.aspx.cs using Method-Based Query Syntax with lambda expressions.

What is the equivalent Method-Based Query Syntax with lambda expression for this snippet?? can anyone help?

            using (NorthwindEntities myEntities = new NorthwindEntities())
            {
                var authorizedEmployees = from employee in myEntities.Employees
                                          //where employee.EmployeeID == 1
                                          orderby employee.EmployeeID ascending                                                                                select new { employee.EmployeeID, employee.FirstName, employee.LastName, employee.Title };
                
                // We canot bind Iqueryable directly. We have to change into ToList()
                GridView1.DataSource = authorizedEmployees.ToList();
 
                GridView1.DataBind();
            }

Open in new window


           
Method-Based Query Syntax with lambda expressions
0
Comment
Question by:maqskywalker
1 Comment
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40423054
Like this:
            using (NorthwindEntities myEntities = new NorthwindEntities())
            {
                var authorizedEmployees = myEntities.Employees.OrderBy(emp => emp.EmployeeID).Select(emp => new { EmployeeID = emp.EmployeeID, FirstName = emp.FirstName, LastName = emp.LastName, Title = emp.Title }).ToList();
                
                // We canot bind Iqueryable directly. We have to change into ToList()
                GridView1.DataSource = authorizedEmployees;
 
                GridView1.DataBind();
            }

Open in new window


-saige-
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question