![]() |
#1 |
Участник
|
gatesasbait: LINQ
Источник: http://gatesasbait.spaces.live.com/B...B9F5!289.entry
============== Источник: http://gatesasbait.spaces.live.com/B...B9F5!289.entry
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
![]() |
#2 |
Участник
|
gatesasbait: LINQ (Part 1, Part 2)
Источник: http://gatesasbait.spaces.live.com/B...B9F5!288.entry
============== Happy New Year! LINQ is used in SSRS reports in AX 2009 and is likely to be used more widely in the upcoming AX 2011. LINQ's purpose is to allow .Net languages to query collections as you would tables and views in SQL. Of course, Microsoft added to that a series of 'LINQ to SQL' capabilities to allow developers to query database tables as if they were collections. Here's the most basic examples showing a select statement running on a standard .Net List collection: Код: using System; using System.Linq; using System.Collections.Generic; using System.Windows.Forms; public class Test { public static void Main() { List aList1 = new List(); aList1.Add(new AnElement() { aMember1 = 5, aMember2 = 6 }); aList1.Add(new AnElement() { aMember1 = 4, aMember2 = 7 }); IEnumerable query = from item in aList1 where item.aMember1 == 5 select item; foreach (AnElement item in query) { MessageBox.Show(item.aMember2.ToString()); } } } public class AnElement { public int aMember1; public int aMember2; } Dynamics Ax developers will notice the inverted syntax of: from item in aList1 where item.aMember1 == 5 select item; instead of select * from A where A.B == C as well as the added 'in' keyword. Part 2 to follow ![]() ![]() Источник: http://gatesasbait.spaces.live.com/B...B9F5!288.entry Источник: http://gatesasbait.spaces.live.com/B...B9F5!289.entry ============== Now to join two collections together: //ref: http://www.hookedonlinq.com/JoinOperator.ashx Код: using System; using System.Linq; using System.Collections.Generic; using System.Windows.Forms; public class Test { public static void Main() { List customers = new List() { new Customer {Key = 1, Name = "Gottshall" }, new Customer {Key = 2, Name = "Valdes" }}; List orders = new List() { new Order {Key = 1, OrderNumber = "Order 1" }, new Order {Key = 1, OrderNumber = "Order 2" }, new Order {Key = 2, OrderNumber = "Order 3" }}; var query = from cust in customers join sale in orders on cust.Key equals sale.Key where cust.Name == "Gottshall" select new { cust.Name, sale.OrderNumber }; foreach (var line in query) { MessageBox.Show(line.Name.ToString()+" "+line.OrderNumber.ToString()); } } } public class Customer { public int Key; public string Name; } public class Order { public int Key; public string OrderNumber; } 'Gottshall Order 1' 'Gottshall Order 2' LINQ is really really cool. Here's why. In my previous post, I had explicitely typed the query with 'IEnumerable'. But here I can't do that, because the resulting join of Customer and Order is of a type that isn't defined, and I'd rather not bother defining a new type for every kind of return I'm going to get. So we use the keyword 'var' to both define the 'IEnumerable' query as well as each of its lines in the foreach loop. 'var' allows .Net to create its own type as needed. It's called 'implicit types'. In theory I'm sure its very different to the Ax 'Object' or 'Common' types, but in practice its pretty close. Part 3 to follow ![]() ![]() Источник: http://gatesasbait.spaces.live.com/B...B9F5!289.entry
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
|
|