Solved

linq to entities Method-Based Query Syntax

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

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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