Basics Flashcards

1
Q

Creating first LINQ Query using Query Syntax in C#

A

Step 1: First add System.Linq namespace in your code.

         using System.Linq;

Step 2: Next, create data source on which you want to perform operations. For example:

List my_list = new List(){
        "This is my Dog",
        "Name of my Dog is Robin",
        "This is my Cat",
        "Name of the cat is Mewmew"
    };

Step 3: Now create the query using the query keywords like select, from, etc. For example:

var res = from l in my_list
              where l.Contains("my")
              select l;

Here res is the query variable which stores the result of the query expression. The from clause is used to specify the data source, i.e, my_list, where clause applies the filter, i.e, l.Contains(“my”) and select clause provides the type of the returned items. And l is the range variable.

Step 4: Last step is to execute the query by using a foreach loop. For example:

foreach(var q in res)
{
Console.WriteLine(q);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Filtering

A
Query Syntax
----------------------
var col = from o in Orders
 where o.CustomerID == 84
 select o;

var col2 = Orders.Where(o => o.CustomerID == 84);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly