Solved

linq to entities Method-Based Query Syntax

Posted on 2014-11-04
1
228 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 32

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 48
Chat Room 1 30
COnsume rest client 6 15
XAML: Layout 8 0
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now