Linq's knowledge sharing

Linq's knowledge sharing

I have come into contact with Linq related knowledge in the recent study, here to explain some of my own understanding, if there is any mistake, please understand

1. Related background introduction

1, due to the different databases or other data storage table lists and so has a different query methods, Linq called Language Integrated Query
2, Linq mainly includes LINQ to Objects, , LINQ to XML, LINQ to SQL, LINQ to Entitywhich objectsrepresents the memory collections, Entityon behalf of ADO.NET Entity Framework(Microsoft's ORM framework)
3. Linq has two ways of writing, one is ordinary writing, the other is Lamda expression
4. The main namespace where LINQ is located: System.Linq

2. Introduction and use of Linq To Objects

1. Query the array

int[] nums = new int[5] {
    
     1, 3, 2, 0, 4 };  //初始化数组
var queryInfo = from num in nums select num;  //遍历数组对象
var queryInfo = from num in nums select num;  //Lambda表达式
string arr = "";
     foreach (var item in queryInfo)
      {
    
    
                arr += item + " ";
      }
      Console.WriteLine(arr);

By the way, the var definition, the actual type of the variable will be pushed out in its initialization expression. (It can only be defined in local variables, Var cannot define global variables, and must be initialized). In the above code, queryInfo is defined as an IEnumerable<T> interface, which receives the return value of the query, and finally traverses the interface to obtain the value through foreach. It is not necessary to use var, as shown below

IEnumerable<int> queryInfo = from num in nums select num;

2. Query the generic linked list

Linked list initialization:

            User user1 = new User() {
    
     UserId = 12231, UserName = "普通成员1" };
            User user2 = new User() {
    
     UserId = 2911, UserName = "普通成员2" };
            User user3 = new User() {
    
     UserId = 3, UserName = "普通成员3" };
            User user4 = new User() {
    
     UserId = 4623, UserName = "普通成员4" };
            User user5 = new User() {
    
     UserId = 5, UserName = "普通成员2" };
            User user6 = new User() {
    
     UserId = 2, UserName = "普通成员2" };
            User user7 = new User() {
    
     UserId = 1, UserName = "普通成员2" };
            List<User> users = new List<User>() {
    
     user1, user2, user3, user4, user5 };    //泛型链表

Use Linq expression to query the UerId whose UserName is ordinary member 1

var User = (from p in users where users.UserName.ToString() == "普通成员1" select users).ToList(); 

Use Lamda expression query

List<User>  User = (users.Where(t => t.UserName=="普通成员1").ToList()

3. Linq queries DataTable

Assuming that dtTable is a DataTable that has been initialized,
use Linq to query:

List<string> lstID = (from d in dtTable.AsEnumerable() select d.Field<string>("ID")).ToList<string>();//查询一个ID字段

Use Lamda expressions

List<string> lstID1 = dtTable.AsEnumerable().Where(t=>t.Field<string>("Name")=="迅雷"|| t.Field<string>("Name")=="谷歌").Select(t => t.Field<string>("ID")).ToList<string>();

3. Introduction and use of Linq To XML 4. Introduction and use
of Linq To SQL
Follow-up update, if it is helpful to yourself, please like and support, thank you

Guess you like

Origin blog.csdn.net/yyq1102394156/article/details/113882093