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
Solved

linq to entities Method-Based Query Syntax

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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 …

856 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