Exemplos de Queries Linq
Alguns exemplos de Queries com Linq, To Object, To XML e To SQL
Exemplos de Queries Linq
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
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 Page< FONT>title>
< FONT>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 Linq< FONT>asp:ListItem>
<asp:ListItem Value="Products">Query Products< FONT>asp:ListItem>
<asp:ListItem Value="Combine">Combine Where() and Select() for common queries< FONT>asp:ListItem>
<asp:ListItem Value="Where">Where< FONT>asp:ListItem>
<asp:ListItem Value="XML">XML< FONT>asp:ListItem>
< FONT>asp:ListBox>
<br />
<br />
<asp:Label ID="lblLinq" runat="server">< FONT>asp:Label>
<br />
<br />
<asp:Label ID="lblQuery" runat="server">< FONT>asp:Label>
<br />
<br />
<asp:GridView ID="gvwResults" runat="server">
< FONT>asp:GridView>
< FONT>ContentTemplate>
< FONT>asp:UpdatePanel>
< FONT>div>
< FONT>form>
< FONT>body>
< FONT>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();
}
}
Artigos relacionados
-
Artigo
-
Artigo
-
Artigo
-
Artigo
-
Artigo