linq xpo w przykładach
Migracja; autor Wojtek, ostatnia modyfikacja przed importem 2017-02-08T16:07:04.000Z; linq-xpo-w-przykładach
Poniżej przykłady użycia linq w DevExpess
// warunkowy if w sekcji where
List<StokHareketDetay> sorgu = (from p in new XPQuery<StokHareketDetay>(XpoDefault.Session)
where p.StokKodu == (string.IsNullOrEmpty(stokkoducol) ? p.StokKodu : stokkoducol
select p).ToList();
// prosty sposób spr. czy pole jest null lub puste
where string.IsNullOrEmpty( o.ContactTitle)
// daje to efekt w SQL
((N0.\"ContactTitle\") is null or len(N0.\"ContactTitle\")
// prosty ekwiwalent polecenia SQL like
// sekcja linq
where o.ID > i && o.ID > 0 && o.CompanyName.Contains("a")"
// przykłady
XPQuery<Orders> orders = new XPQuery<Order>( XpoDefault.Session );
XPQuery<Employees> employees = new XPQuery<Employees>( XpoDefault.Session );
var xOrders = from o in orders
where orders.Any(x => employees.Any(s => s.EmployeeID == x.OrderID))
select o;
using System.Linq;
using System.Linq.Expressions;
using DevExpress.Xpo;
using DevExpress.Xpo.Linq;
XPQuery<Customer> customers = Session.DefaultSession.Query<Customer>();
XPQuery<Order> orders = Session.DefaultSession.Query<Order>();
XPQuery<Employee> employees = Session.DefaultSession.Query<Employee>();
// Simple Select with Where and OrderBy clauses
var list = from c in customers
where (c.Country == "Germany" && c.ContactTitle == "Sales Representative")
orderby c.ContactName
select c;
foreach (Customer cust in list)
Console.WriteLine(string.Format("{0}\t{1}\t{2}", cust.ContactName,
cust.Country, cust.ContactTitle));
// Select Top 5 objects
var list = (from o in orders
orderby o.ShippedDate descending
select o).Take(5);
foreach (Order order in list)
Console.WriteLine(string.Format("{0}\t{1}\t{2}", order.OrderID, order.ShippedDate,
order.Customer.CompanyName));
// Group Join customers with an aggregation on their Orders
var list = from c in customers
join o in orders on c equals o.Customer into oo
where oo.Count() >= 1
select new { c.CompanyName, OrderCount = oo.Count() };
foreach (var item in list)
Console.WriteLine(string.Format("{0}\t{1}", item.CompanyName, item.OrderCount));
// An example of aggregated functions (Count and Average)
var list = from o in orders
select o;
int count = list.Count();
Console.WriteLine(string.Format("Orders Row Count: {0}", count));
decimal avg = list.Average(x => x.Freight);
Console.WriteLine(string.Format("Orders Average Freight: {0:c2}", avg));
// Select with Group By
var list = from c in customers
group c by c.ContactTitle into cc
where cc.Count() >= 1
select new { Title = cc.Key, Count = cc.Count() };
foreach (var item in list)
Console.WriteLine(string.Format("{0}\t{1}", item.Title, item.Count));
// Any method
bool result = customers.Any(c => c.Country == "Spain");
Console.WriteLine(string.Format("Is there any customer from Spain? {0}", result ? "Yes" : "No"));
result = customers.Any(c => c.Country == "Monaco");
Console.WriteLine(string.Format("Is there any customer from Monaco? {0}", result ? "Yes" : "No"));
// Przykład wywołania z join z warunkiem łączonym
var qFak = from z in new XPQuery<YTM_Zadanie_PV3_Faktura>(XpoDefault.Session)
join f in new XPQuery<YFaktura>(XpoDefault.Session)
on new { Numer = z.YTMZPV3F_Numer.Trim(), Lp = z.YTMZPV3F_LpFV }
equals
new { Numer = f.YFA_Seria.Trim() + "/" + f.YFA_Numer.ToString() + "/" + f.YFA_RokFin, Lp = f.YFA_Numer }
where z.YTMZPV3F_IdZadanie == xZad &&
(z.YTMZPV3F_Anulowano == null || z.YTMZPV3F_Anulowano == 0) &&
(f.YFA_Zaksiegowana == null || f.YFA_Zaksiegowana == 0)
select z;