<< Click to Display Table of Contents >> Linq com DataSet |
![]() ![]() ![]() |
Tela
aspx
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</form>
</body>
cs
using System;
using System.Data;
namespace WebApplication5
{
public partial class WebForm1 : System.Web.UI.Page
{
private DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Carregar();
// usando LINQ num DataSet pré-concebido no método anterior
var subQuery = (from clientes in dt.AsEnumerable()
// somente clientes com salario maior que 260
where Convert.ToDecimal(clientes["salario"]) > 260
// ordena
orderby clientes.Field<string>("nome") descending, clientes.Field<DateTime>("data")
select new
{
Codigo = clientes.Field<int>("id"),
Nome = clientes.Field<string>("nome"),
Nasc = clientes.Field<DateTime>("data"),
Salario = clientes.Field<decimal>("salario")
});
GridView1.DataSource = subQuery;
GridView1.DataBind();
}
}
private void CriarDataTable()
{
dt = new DataTable("tb_dados");
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("nome", typeof(string));
dt.Columns.Add("data", typeof(DateTime));
dt.Columns.Add("salario", typeof(decimal));
}
private void AddRow(int id, string nome, DateTime data, decimal salario)
{
DataRow ret = dt.NewRow();
ret["id"] = id;
ret["nome"] = nome;
ret["data"] = data;
ret["salario"] = salario;
dt.Rows.Add(ret);
}
private void Carregar()
{
CriarDataTable();
AddRow(1, "Mel", new DateTime(1977, 7, 12), (decimal)1000.10);
AddRow(2, "Natalia", new DateTime(1988, 10, 2), (decimal)2200.00);
AddRow(3, "Luisa", new DateTime(1991, 1, 1), (decimal)50.24);
AddRow(4, "Kellen", new DateTime(1990, 2, 12), (decimal)1.10);
AddRow(5, "Danieli", new DateTime(1965, 7, 20), (decimal)0.09);
}
}
}