img
 
 

Exemplos de Queries Linq


DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Pagetitle>

head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server" />

<div>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<asp:ListBox ID="lstOption" runat="server" AutoPostBack="True"

onselectedindexchanged="lstOption_SelectedIndexChanged" Height="165px"

Width="351px">

<asp:ListItem Value="Hello">Hello Linqasp:ListItem>

<asp:ListItem Value="Products">Query Productsasp:ListItem>

<asp:ListItem Value="Combine">Combine Where() and Select() for common queriesasp:ListItem>

<asp:ListItem Value="Where">Whereasp:ListItem>

<asp:ListItem Value="XML">XMLasp:ListItem>

asp:ListBox>

<br />

<br />

<asp:Label ID="lblLinq" runat="server">asp:Label>

<br />

<br />

<asp:Label ID="lblQuery" runat="server">asp:Label>

<br />

<br />

<asp:GridView ID="gvwResults" runat="server">

asp:GridView>

ContentTemplate>

asp:UpdatePanel>

div>

form>

body>

html>

 
 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page

{

NorthwindDataContext db = new NorthwindDataContext();

protected void Page_Load(object sender, EventArgs e)

{

}

protected void lstOption_SelectedIndexChanged(object sender, EventArgs e)

{

lblQuery.Text = String.Empty;

lblLinq.Text = String.Empty;

SelectLinq();

}

private void SelectLinq()

{

string strOption = lstOption.SelectedValue;

switch (strOption)

{

case "Hello":

Hello();

break;

case "Products":

Products();

break;

case "Combine":

Combine();

break;

case "Where":

Where();

break;

case "XML":

XML();

break;

default:

break;

}

}

private void Hello()

{

string[] greetings = { "hello world", "hello LINQ", "hello ASPNETi" };

 

//Coleção

IEnumerable<string> q = from s in greetings

where s.EndsWith("LINQ")

select s;

lblLinq.Text = "string[] greetings = { \"hello world\", \"hello LINQ\", \"hello ASPNETi\" };";

lblQuery.Text = "IEnumerable<string> q = from s in greetings
where s.EndsWith(\"LINQ\")
select s"
;

gvwResults.DataSource = q;

gvwResults.DataBind();

}

 

private void Combine()

{

// Combine Where() and Select() for common queries

IQueryable<string> q = db.Customers.Where(c => c.City == "London").Select(c => c.ContactName);

lblLinq.Text = q.ToString();

string strLinq = "Queryable<string> q = db.Customers.Where(c => c.City == \"London\").Select(c => c.ContactName);";

lblQuery.Text = strLinq;

gvwResults.DataSource = q;

gvwResults.DataBind();

}

 

 

private void Products()

{

IQueryable<Product> q = from p in db.Products

where p.Category.CategoryName == "Beverages"

select p;

lblLinq.Text = q.ToString();

string strLinq = "IQueryable<Product> q = from p in db.Products
where p.Category.CategoryName == \"Beverages\"
select p;"
;

lblQuery.Text = strLinq;

gvwResults.DataSource = q;

gvwResults.DataBind();

}

 

private void Where()

{

IQueryable<Product> q = from p in db.Products

where p.UnitsInStock == 0

select p;

lblLinq.Text = q.ToString();

string strLinq = "IQueryable<Product> q = from p in db.Products where p.UnitsInStock == 0 select p;";

lblQuery.Text = strLinq;

gvwResults.DataSource = q;

gvwResults.DataBind();

}

 

private void XML()

{

XElement books = XElement.Parse(

@"

var q =

from book in books.Elements("book")

where (string)book.Element("author") == "Joe Rattz"

select new { Title = book.Element("title").Value, Author = book.Element("author").Value };

 

lblQuery.Text = "var q =
from book in books.Elements(\"book\")
where (string)book.Element(\"author\") == \"Joe Rattz\"
select new { Title = book.Element(\"title\").Value, Author = book.Element(\"author\").Value };"
;

gvwResults.DataSource = q;

gvwResults.DataBind();

}

 

}

 
Fabio Galante Mans

fabio@netitc.com.br - Hospedagem para desenvolvedores