zoukankan      html  css  js  c++  java
  • .NET复习笔记-Linq之联合查询示例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace WinningConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<User> userList = InitUserList();
                List<Student> studentList = InitStudentList();
    
                var result1 = from user in userList
                              join student in studentList on user.ID equals student.UserID
                              where user.ID > 1
                              select new { UserID = user.ID, user.Name, student.ClassName };
    
                foreach (var item in result1)
                {
                    Console.WriteLine("UserID:{0}, Name:{1}, ClassName:{2}", item.UserID, item.Name, item.ClassName);
                }
    
                Console.ReadKey();
            }
    
            private static List<User> InitUserList()
            {
                return new List<User>() {
    
                    new User{ ID = 1, Name = "name1", Age = 13 },
                    new User{ ID = 2, Name = "name2", Age = 14 },
                    new User{ ID = 3, Name = "name3", Age = 15 }
                };
            }
    
            private static List<Student> InitStudentList()
            {
                return new List<Student>() {
                    new Student { ID = 1, UserID = 1, ClassName = "className1" },
                    new Student { ID = 2, UserID = 2, ClassName = "className1" },
                    new Student { ID = 3, UserID = 3, ClassName = "className2" }
                };
            }
        }
    
        internal class Student
        {
            internal int ID { get; set; }
            internal int UserID { get; set; }
            internal string ClassName { get; set; }
    
        }
        
        internal class User
        {
            internal int ID { get; set; }
            internal string Name { get; set; }
            internal int Age { get; set; }
        }
    }
    
    /*输出
    UserID:2, Name:name2, ClassName:className1
    UserID:3, Name:name3, ClassName:className2
    */
    

      

  • 相关阅读:
    Unity之串口通信(基于三姿态传感器)
    Unity3d win7协议多点触控
    大大的蛋项目 第二篇 第三关
    大大的蛋项目
    Unity3d 调用C++的DLL
    有梦想的小鸟
    【Unity3D插件】NGUI屏幕自适应 .
    【Unity3d】使GUI适应屏幕分辨率
    unity自动保存项目
    BloomFilter——大规模数据处理利器
  • 原文地址:https://www.cnblogs.com/ironcrow/p/10928189.html
Copyright © 2011-2022 走看看