EDN Admin
Well-known member
Hi, I am having difficulties to understand the labda expression with 3 parameters.
Basically I need to use LINQ join as a lambda expression.
What I am trying to achieve is to write the same query that starts on line 23 with lambda expression syntax on line 30.
<div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white
<table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0>
<colgroup>
<col style="border-right:gray 1px solid;padding-right:5px;padding-left:10px;font-size:11px;vertical-align:top;width:5px;color:gray;font-family:Courier New;background-color:#eee;text-align:right
<col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap
<tbody>
<tr>
<td><nobr>1</nobr></td>
<td><font style="font-size:11px public class Client </font></td></tr>
<tr>
<td><nobr>2</nobr></td>
<td style="background-color:#f7f7f7 { </td></tr>
<tr>
<td><nobr>3</nobr></td>
<td> public string Id { get; set; } </td></tr>
<tr>
<td><nobr>4</nobr></td>
<td style="background-color:#f7f7f7 } </td></tr>
<tr>
<td><nobr>5</nobr></td>
<td> public class Order </td></tr>
<tr>
<td><nobr>6</nobr></td>
<td style="background-color:#f7f7f7 { </td></tr>
<tr>
<td><nobr>7</nobr></td>
<td> public string Id { get; set; } </td></tr>
<tr>
<td><nobr>8</nobr></td>
<td style="background-color:#f7f7f7 public string ClientId { get; set; } </td></tr>
<tr>
<td><nobr>9</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>10</nobr></td>
<td style="background-color:#f7f7f7 public class DataBase </td></tr>
<tr>
<td><nobr>11</nobr></td>
<td> { </td></tr>
<tr>
<td><nobr>12</nobr></td>
<td style="background-color:#f7f7f7 public List</font><font style="color:blue <</font><font style="font-size:11px Client</font><font style="color:blue ></font><font style="font-size:11px Clients { get; set; } </font></td></tr>
<tr>
<td><nobr>13</nobr></td>
<td> public List</font><font style="color:blue <</font><font style="font-size:11px Order</font><font style="color:blue ></font><font style="font-size:11px Orders { get; set; } </font></td></tr>
<tr>
<td><nobr>14</nobr></td>
<td style="background-color:#f7f7f7 } </td></tr>
<tr>
<td><nobr>15</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>16</nobr></td>
<td style="background-color:#f7f7f7 public class Program </td></tr>
<tr>
<td><nobr>17</nobr></td>
<td> { </td></tr>
<tr>
<td><nobr>18</nobr></td>
<td style="background-color:#f7f7f7 static void Main(string[] args) </td></tr>
<tr>
<td><nobr>19</nobr></td>
<td> { </td></tr>
<tr>
<td><nobr>20</nobr></td>
<td style="background-color:#f7f7f7 // get database </td></tr>
<tr>
<td><nobr>21</nobr></td>
<td> var </font><font style="color:red db</font><font style="font-size:11px = </font><font style="color:blue GetDataBase</font><font style="font-size:11px (); </font></td></tr>
<tr>
<td><nobr>22</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>23</nobr></td>
<td> var </font><font style="color:red query</font><font style="font-size:11px = </font><font style="color:blue from</font><font style="font-size:11px clients in db.Clients </font></td></tr>
<tr>
<td><nobr>24</nobr></td>
<td style="background-color:#f7f7f7 join orders in db.Orders on clients.Id equals orders.ClientId </td></tr>
<tr>
<td><nobr>25</nobr></td>
<td> select new { </font><font style="color:red Clients</font><font style="font-size:11px = </font><font style="color:blue clients</font><font style="font-size:11px , </font><font style="color:red Orders</font><font style="font-size:11px = </font><font style="color:blue orders</font><font style="font-size:11px }; </font></td></tr>
<tr>
<td><nobr>26</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>27</nobr></td>
<td> // </td></tr>
<tr>
<td><nobr>28</nobr></td>
<td style="background-color:#f7f7f7 // HOW TO WRITE IT AS A LAMBDA EXPRESSION ?????? </td></tr>
<tr>
<td><nobr>29</nobr></td>
<td> // </td></tr>
<tr>
<td><nobr>30</nobr></td>
<td style="background-color:#f7f7f7 //var </font><font style="color:red query</font><font style="font-size:11px = </font><font style="color:blue db</font><font style="font-size:11px .Clients.Join(db.Orders, </font><font style="color:red client</font><font style="font-size:11px =</font><font style="color:blue ></font><font style="font-size:11px client.Id, </font><font style="color:red order</font><font style="font-size:11px =</font><font style="color:blue ></font><font style="font-size:11px order.ClientId, client </font></td></tr>
<tr>
<td><nobr>31</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>32</nobr></td>
<td style="background-color:#f7f7f7 foreach (var item in query) </td></tr>
<tr>
<td><nobr>33</nobr></td>
<td> { </td></tr>
<tr>
<td><nobr>34</nobr></td>
<td style="background-color:#f7f7f7 Console.WriteLine(string.Format("</font><font style="color:red ClientId</font><font style="font-size:11px = {0}, </font><font style="color:red OrderId</font><font style="font-size:11px = {1}", item.Clients.Id, item.Orders.Id)); </font></td></tr>
<tr>
<td><nobr>35</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>36</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>37</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>38</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>39</nobr></td>
<td> private static DataBase GetDataBase() </td></tr>
<tr>
<td><nobr>40</nobr></td>
<td style="background-color:#f7f7f7 { </td></tr>
<tr>
<td><nobr>41</nobr></td>
<td> // create clients </td></tr>
<tr>
<td><nobr>42</nobr></td>
<td style="background-color:#f7f7f7 var </font><font style="color:red client1</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Client() { </font><font style="color:red Id</font><font style="font-size:11px = </font><font style="color:blue "Client 1"</font><font style="font-size:11px }; </font></td></tr>
<tr>
<td><nobr>43</nobr></td>
<td> var </font><font style="color:red client2</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Client() { </font><font style="color:red Id</font><font style="font-size:11px = </font><font style="color:blue "Client 2"</font><font style="font-size:11px }; </font></td></tr>
<tr>
<td><nobr>44</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>45</nobr></td>
<td> // create orders and add them to client 1 </td></tr>
<tr>
<td><nobr>46</nobr></td>
<td style="background-color:#f7f7f7 var </font><font style="color:red order1</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Order() { </font><font style="color:red Id</font><font style="font-size:11px = </font><font style="color:blue "Order 1"</font><font style="font-size:11px , </font><font style="color:red ClientId</font><font style="font-size:11px = </font><font style="color:blue client1</font><font style="font-size:11px .Id }; </font></td></tr>
<tr>
<td><nobr>47</nobr></td>
<td> var </font><font style="color:red order2</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Order() { </font><font style="color:red Id</font><font style="font-size:11px = </font><font style="color:blue "Order 2"</font><font style="font-size:11px , </font><font style="color:red ClientId</font><font style="font-size:11px = </font><font style="color:blue client1</font><font style="font-size:11px .Id }; </font></td></tr>
<tr>
<td><nobr>48</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>49</nobr></td>
<td> // create order without client id </td></tr>
<tr>
<td><nobr>50</nobr></td>
<td style="background-color:#f7f7f7 var </font><font style="color:red order3</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Order() { </font><font style="color:red Id</font><font style="font-size:11px = </font><font style="color:blue "Order 3"</font><font style="font-size:11px }; </font></td></tr>
<tr>
<td><nobr>51</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>52</nobr></td>
<td style="background-color:#f7f7f7 // create database </td></tr>
<tr>
<td><nobr>53</nobr></td>
<td> var </font><font style="color:red db</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px DataBase() { </font><font style="color:red Clients</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px List</font><font style="color:blue <</font><font style="font-size:11px Client</font><font style="color:blue ></font><font style="font-size:11px (), </font><font style="color:red Orders</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px List</font><font style="color:blue <</font><font style="font-size:11px Order</font><font style="color:blue ></font><font style="font-size:11px () }; </font></td></tr>
<tr>
<td><nobr>54</nobr></td>
<td style="background-color:#f7f7f7 db.Clients.Add(client1); </td></tr>
<tr>
<td><nobr>55</nobr></td>
<td> db.Clients.Add(client2); </td></tr>
<tr>
<td><nobr>56</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>57</nobr></td>
<td> db.Orders.Add(order1); </td></tr>
<tr>
<td><nobr>58</nobr></td>
<td style="background-color:#f7f7f7 db.Orders.Add(order2); </td></tr>
<tr>
<td><nobr>59</nobr></td>
<td> db.Orders.Add(order3); </td></tr>
<tr>
<td><nobr>60</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>61</nobr></td>
<td> return db; </td></tr>
<tr>
<td><nobr>62</nobr></td>
<td style="background-color:#f7f7f7 } </td></tr>
<tr>
<td><nobr>63</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>64</nobr></td>
<td style="background-color:#f7f7f7 </td></tr></tbody></table>
Thanks,
Edijs
View the full article
Basically I need to use LINQ join as a lambda expression.
What I am trying to achieve is to write the same query that starts on line 23 with lambda expression syntax on line 30.
<div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white
<table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0>
<colgroup>
<col style="border-right:gray 1px solid;padding-right:5px;padding-left:10px;font-size:11px;vertical-align:top;width:5px;color:gray;font-family:Courier New;background-color:#eee;text-align:right
<col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap
<tbody>
<tr>
<td><nobr>1</nobr></td>
<td><font style="font-size:11px public class Client </font></td></tr>
<tr>
<td><nobr>2</nobr></td>
<td style="background-color:#f7f7f7 { </td></tr>
<tr>
<td><nobr>3</nobr></td>
<td> public string Id { get; set; } </td></tr>
<tr>
<td><nobr>4</nobr></td>
<td style="background-color:#f7f7f7 } </td></tr>
<tr>
<td><nobr>5</nobr></td>
<td> public class Order </td></tr>
<tr>
<td><nobr>6</nobr></td>
<td style="background-color:#f7f7f7 { </td></tr>
<tr>
<td><nobr>7</nobr></td>
<td> public string Id { get; set; } </td></tr>
<tr>
<td><nobr>8</nobr></td>
<td style="background-color:#f7f7f7 public string ClientId { get; set; } </td></tr>
<tr>
<td><nobr>9</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>10</nobr></td>
<td style="background-color:#f7f7f7 public class DataBase </td></tr>
<tr>
<td><nobr>11</nobr></td>
<td> { </td></tr>
<tr>
<td><nobr>12</nobr></td>
<td style="background-color:#f7f7f7 public List</font><font style="color:blue <</font><font style="font-size:11px Client</font><font style="color:blue ></font><font style="font-size:11px Clients { get; set; } </font></td></tr>
<tr>
<td><nobr>13</nobr></td>
<td> public List</font><font style="color:blue <</font><font style="font-size:11px Order</font><font style="color:blue ></font><font style="font-size:11px Orders { get; set; } </font></td></tr>
<tr>
<td><nobr>14</nobr></td>
<td style="background-color:#f7f7f7 } </td></tr>
<tr>
<td><nobr>15</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>16</nobr></td>
<td style="background-color:#f7f7f7 public class Program </td></tr>
<tr>
<td><nobr>17</nobr></td>
<td> { </td></tr>
<tr>
<td><nobr>18</nobr></td>
<td style="background-color:#f7f7f7 static void Main(string[] args) </td></tr>
<tr>
<td><nobr>19</nobr></td>
<td> { </td></tr>
<tr>
<td><nobr>20</nobr></td>
<td style="background-color:#f7f7f7 // get database </td></tr>
<tr>
<td><nobr>21</nobr></td>
<td> var </font><font style="color:red db</font><font style="font-size:11px = </font><font style="color:blue GetDataBase</font><font style="font-size:11px (); </font></td></tr>
<tr>
<td><nobr>22</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>23</nobr></td>
<td> var </font><font style="color:red query</font><font style="font-size:11px = </font><font style="color:blue from</font><font style="font-size:11px clients in db.Clients </font></td></tr>
<tr>
<td><nobr>24</nobr></td>
<td style="background-color:#f7f7f7 join orders in db.Orders on clients.Id equals orders.ClientId </td></tr>
<tr>
<td><nobr>25</nobr></td>
<td> select new { </font><font style="color:red Clients</font><font style="font-size:11px = </font><font style="color:blue clients</font><font style="font-size:11px , </font><font style="color:red Orders</font><font style="font-size:11px = </font><font style="color:blue orders</font><font style="font-size:11px }; </font></td></tr>
<tr>
<td><nobr>26</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>27</nobr></td>
<td> // </td></tr>
<tr>
<td><nobr>28</nobr></td>
<td style="background-color:#f7f7f7 // HOW TO WRITE IT AS A LAMBDA EXPRESSION ?????? </td></tr>
<tr>
<td><nobr>29</nobr></td>
<td> // </td></tr>
<tr>
<td><nobr>30</nobr></td>
<td style="background-color:#f7f7f7 //var </font><font style="color:red query</font><font style="font-size:11px = </font><font style="color:blue db</font><font style="font-size:11px .Clients.Join(db.Orders, </font><font style="color:red client</font><font style="font-size:11px =</font><font style="color:blue ></font><font style="font-size:11px client.Id, </font><font style="color:red order</font><font style="font-size:11px =</font><font style="color:blue ></font><font style="font-size:11px order.ClientId, client </font></td></tr>
<tr>
<td><nobr>31</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>32</nobr></td>
<td style="background-color:#f7f7f7 foreach (var item in query) </td></tr>
<tr>
<td><nobr>33</nobr></td>
<td> { </td></tr>
<tr>
<td><nobr>34</nobr></td>
<td style="background-color:#f7f7f7 Console.WriteLine(string.Format("</font><font style="color:red ClientId</font><font style="font-size:11px = {0}, </font><font style="color:red OrderId</font><font style="font-size:11px = {1}", item.Clients.Id, item.Orders.Id)); </font></td></tr>
<tr>
<td><nobr>35</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>36</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>37</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>38</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>39</nobr></td>
<td> private static DataBase GetDataBase() </td></tr>
<tr>
<td><nobr>40</nobr></td>
<td style="background-color:#f7f7f7 { </td></tr>
<tr>
<td><nobr>41</nobr></td>
<td> // create clients </td></tr>
<tr>
<td><nobr>42</nobr></td>
<td style="background-color:#f7f7f7 var </font><font style="color:red client1</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Client() { </font><font style="color:red Id</font><font style="font-size:11px = </font><font style="color:blue "Client 1"</font><font style="font-size:11px }; </font></td></tr>
<tr>
<td><nobr>43</nobr></td>
<td> var </font><font style="color:red client2</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Client() { </font><font style="color:red Id</font><font style="font-size:11px = </font><font style="color:blue "Client 2"</font><font style="font-size:11px }; </font></td></tr>
<tr>
<td><nobr>44</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>45</nobr></td>
<td> // create orders and add them to client 1 </td></tr>
<tr>
<td><nobr>46</nobr></td>
<td style="background-color:#f7f7f7 var </font><font style="color:red order1</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Order() { </font><font style="color:red Id</font><font style="font-size:11px = </font><font style="color:blue "Order 1"</font><font style="font-size:11px , </font><font style="color:red ClientId</font><font style="font-size:11px = </font><font style="color:blue client1</font><font style="font-size:11px .Id }; </font></td></tr>
<tr>
<td><nobr>47</nobr></td>
<td> var </font><font style="color:red order2</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Order() { </font><font style="color:red Id</font><font style="font-size:11px = </font><font style="color:blue "Order 2"</font><font style="font-size:11px , </font><font style="color:red ClientId</font><font style="font-size:11px = </font><font style="color:blue client1</font><font style="font-size:11px .Id }; </font></td></tr>
<tr>
<td><nobr>48</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>49</nobr></td>
<td> // create order without client id </td></tr>
<tr>
<td><nobr>50</nobr></td>
<td style="background-color:#f7f7f7 var </font><font style="color:red order3</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px Order() { </font><font style="color:red Id</font><font style="font-size:11px = </font><font style="color:blue "Order 3"</font><font style="font-size:11px }; </font></td></tr>
<tr>
<td><nobr>51</nobr></td>
<td> </td></tr>
<tr>
<td><nobr>52</nobr></td>
<td style="background-color:#f7f7f7 // create database </td></tr>
<tr>
<td><nobr>53</nobr></td>
<td> var </font><font style="color:red db</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px DataBase() { </font><font style="color:red Clients</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px List</font><font style="color:blue <</font><font style="font-size:11px Client</font><font style="color:blue ></font><font style="font-size:11px (), </font><font style="color:red Orders</font><font style="font-size:11px = </font><font style="color:blue new</font><font style="font-size:11px List</font><font style="color:blue <</font><font style="font-size:11px Order</font><font style="color:blue ></font><font style="font-size:11px () }; </font></td></tr>
<tr>
<td><nobr>54</nobr></td>
<td style="background-color:#f7f7f7 db.Clients.Add(client1); </td></tr>
<tr>
<td><nobr>55</nobr></td>
<td> db.Clients.Add(client2); </td></tr>
<tr>
<td><nobr>56</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>57</nobr></td>
<td> db.Orders.Add(order1); </td></tr>
<tr>
<td><nobr>58</nobr></td>
<td style="background-color:#f7f7f7 db.Orders.Add(order2); </td></tr>
<tr>
<td><nobr>59</nobr></td>
<td> db.Orders.Add(order3); </td></tr>
<tr>
<td><nobr>60</nobr></td>
<td style="background-color:#f7f7f7 </td></tr>
<tr>
<td><nobr>61</nobr></td>
<td> return db; </td></tr>
<tr>
<td><nobr>62</nobr></td>
<td style="background-color:#f7f7f7 } </td></tr>
<tr>
<td><nobr>63</nobr></td>
<td> } </td></tr>
<tr>
<td><nobr>64</nobr></td>
<td style="background-color:#f7f7f7 </td></tr></tbody></table>
Thanks,
Edijs
View the full article