Il framework è il 4.0
l'ambiente e Vs 2010.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqTest
{
class Program
{
public class OrderRow
{
public int amount { get; set; }
public string status { get; set; }
}
public class Order
{
public string name { get; set; }
public DateTime date { get; set; }
public int cost { get; set; }
public List<orderrow> Rows = new List<orderrow>();
}
static void Main(string[] args)
{
Order i1 = new Order();
i1.name = "001/1";
i1.date = DateTime.Now;
i1.cost = 100;
OrderRow i1_r1 = new OrderRow();
OrderRow i1_r2 = new OrderRow();
OrderRow i1_r3 = new OrderRow();
OrderRow i1_r4 = new OrderRow();
i1_r1.amount = 10; i1_r1.status = "s";
i1_r2.amount = 20; i1_r2.status = "s";
i1_r3.amount = 30; i1_r3.status = "s";
i1_r4.amount = 40; i1_r4.status = "s";
i1.Rows.Add(i1_r1);
i1.Rows.Add(i1_r2);
i1.Rows.Add(i1_r3);
i1.Rows.Add(i1_r4);
Order i2 = new Order();
i2.name = "001/2";
i2.date = DateTime.Now;
i2.cost = 10;
OrderRow i2_r1 = new OrderRow();
OrderRow i2_r2 = new OrderRow();
i2_r1.amount = 4; i2_r1.status = "n";
i2_r2.amount = 6; i2_r2.status = "n";
i2.Rows.Add(i2_r1);
i2.Rows.Add(i2_r2);
Order i3 = new Order();
i3.name = "001/3";
i3.date = DateTime.Now;
i3.cost = 1000;
OrderRow i3_r1 = new OrderRow();
i3_r1.amount = 990; i3_r1.status = "n";
i3.Rows.Add(i3_r1);
Order i4 = new Order();
i4.name = "001/4";
i4.date = DateTime.Now;
i4.cost = 1200;
Order[] Orders = new Order[] { i1, i2, i3, i4 };
var cResult = from t in Orders
where t.cost >= 100
select t;
foreach (var x in cResult)
{
Console.WriteLine(x.name + " " + x.cost.ToString());
}
cResult = from t in Orders
where (t.cost >= 100) && (t.Rows.Count > 2)
select t;
foreach (var x in cResult)
{
Console.WriteLine(x.name + " " + x.cost.ToString());
}
cResult = from t in Orders
where (t.cost == 100) && (t.Rows.Sum(c=> c.amount) == 100)
select t;
foreach (var x in cResult)
{
Console.WriteLine(x.name + " " + x.cost.ToString());
}
cResult = from t in Orders
where t.cost == (t.Rows.Sum(c => c.amount) )
select t;
foreach (var x in cResult)
{
Console.WriteLine(x.name + " " + x.cost.ToString() );
}
Console.ReadLine();
}
}
}
E dato che qualcuno al tempo disse " quando trovo un esempio su web difficilemente va ... " questo va !
Nessun commento:
Posta un commento