?
Solved

linq to entities Method-Based Query Syntax

Posted on 2014-11-04
1
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 34

Accepted Solution

by:
it_saige earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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