On Linq query

A .Var keywords

Before learning Linq query, we first learn to use var keyword, take a look at Microsoft's official definition: from start Visual C # 3.0, the variable range of the method may have an implicit declaration of "type"  var. Implicitly typed local variable is strongly typed , just like your own declare as type, but the compiler determines the type . From this definition we have two points to note, first declare a local variable with the var implicit type is strongly typed, two .var inferred type is determined at compile time, not at run time.

Look at two examples given Microsoft's official:

// Example #1: var is optional when
// the select clause specifies a string
string[] words = { "apple", "strawberry", "grape", "peach", "banana" };
var wordQuery = from word in words
                where word[0] == 'g'
                select word;

// Because each element in the sequence is a string, 
// not an anonymous type, var is optional here also.
foreach (string s in wordQuery)
{
    Console.WriteLine(s);
}

varAllows use but not required, because the type of query results can clearly be expressed asIEnumerable<string>

// Example #2: var is required because
// the select clause specifies an anonymous type
var custQuery = from cust in customers
                where cust.City == "Phoenix"
                select new { cust.Name, cust.Phone };

// var must be used because each item 
// in the sequence is an anonymous type
foreach (var item in custQuery)
{
    Console.WriteLine("Name={0}, Phone={1}", item.Name, item.Phone);
}

varAllowing the result is a collection of anonymous types, in addition to the compiler itself, not the name of this type of access. Use eliminated to create a new category for the result required .var

 In the second example, if no var keyword, then we will create a good class in advance, query for a change, will change with the query results, but also to define new types, conditions linq query is free to change, impossible conditions a change to a new type of statement, so the var keyword query to linq essential.

II. Anonymous class

 Create an object must first define the type of the object Why, of course not, we can create objects by anonymous class way, look at the following code:

  static void Main(string[] args)
        {
            string name = "Tom";
            int age = 20;
            var obj = new { name,age };
            Console.WriteLine(obj.age.ToString() + obj.name.ToString());
            Console.Read();

        }

Three .Linq inquiry

Linq query expansion method is essentially a series of set of operations, core interface Linq property is IEnumerable, IEnumerable only to achieve a set of interfaces can use Linq query.

Linq query contains three methods:

1. Using the syntax of the query, the query SQL statements and more like:

// Query #1.
            List<int> objInt = new List<int> { 1, 2, 5, 4, 3, 9, 7, 6, 11, 29, 99 };
            var result = from i in objInt
                         where i > 3 && i < 20
                         orderby i descending
                         select i;

            foreach (var item in result)
            {
                Console.WriteLine(item.ToString());
            }

            Console.Read();

            // Query #2.
            List<string> objListString = new List<string> { "abc", "acd", "bcv", "bcd", "cnh", "ckl", "glk" };
            var strResult = from str in objListString
                            group str by str[0];
            foreach (var item in strResult)
            {
                foreach (var item1 in item)
                {
                    Console.WriteLine(item1);
                }
            }

            Console.Read();

        }

2. Use a query expansion method:

Some methods must query the performance of method calls, the most common of such methods is to return a single numerical methods, such as  Sum , Max , Min , Average  and so on. These methods must always be the last call in any query because they represent only a single value, it can not be used as a source for other query operations.

Select extension method of use:

 static void Main(string[] args)
        {
            int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

            var list = nums.Select(item => item * item);
            foreach (int item in list)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }

Where extension method of use:

  static void Main(string[] args)
        {
            int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

            var list = nums
                .Where(item => item % 2 == 0)
                .Select(i => i * i);

            Console.ReadLine();
        }

Extension methods orderby usage:

        static void Main(string[] args)
        {
            int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };
            var list = nums
                .Where(item => item % 2 == 0)
                .Select(i => i * i)
                .OrderBy(item => item);
            foreach (int i in list)
            {
                Console.WriteLine(i);
            }

            Console.ReadLine();
        }

the Main void static (String [] args)
{
String [] = {the nums "Yong", "Wang Qi", "Liu Jing", "Zhao Xinxin",
"Du", "Ma only", "Na Ying", " Chan ",};

 
 

var list = nums
.Where(item => item.Length == 2)
.Select(item => item)
.OrderByDescending(item => item.Substring(0, 1));
foreach (string item in list)
{
Console.WriteLine(item);
}

 
 

Console.ReadLine();
}

3. Combination use query syntax and method syntax:

int numCount1 =
    (from num in numbers1
     where num < 3 || num > 7
     select num).Count();

Because the query returns a single value instead of a collection, so the query execution immediately .

 

Guess you like

Origin www.cnblogs.com/Artist007/p/11076671.html