C#------LINQ查询(一)

1.查询一定范围数字

复制代码
        static void QueryInt()
        {
            // Specify the data source.
            int[] scores = { 97, 92, 81, 60 };

            // Define the query expression.
            IEnumerable<int> scoreQuery =
                from score in scores
                where score > 90
                select score;

            // Execute the query.
            foreach (int i in scoreQuery)
            {
                Console.Write(i + " ");
            }

            // Output: 97 92
        }
复制代码

2.查询包含指定字符的字符

复制代码
        static void QueryString()
        {
            // Specify the data source.
            string[] scores = { "SU001", "SU003", "EFUN", "ECHO" };

            // Define the query expression.
            IEnumerable<string> scoreQuery =
                from score in scores
                where score.StartsWith("SU")
                select score;

            // Execute the query.
            foreach (string i in scoreQuery)
            {
                Console.Write(i + " ");
            }

            // Output: SU001  SU003
        }
复制代码

3.查询指定条件对象

复制代码
    public class StudentScore
    { 
        public string StudentName { get; set; }

        public int MathScore { get; set; }

        public int EnglishScore { get; set; }

        public int ChineseScore { get; set; }


    }
复制代码
复制代码
        static void Main(string[] args)
        {
            //QueryInt();
            //QueryString();

            List<StudentScore> students = new List<StudentScore> 
            { 
                new StudentScore(){StudentName="SU001",MathScore=95,EnglishScore = 100,ChineseScore = 99},
                new StudentScore(){StudentName="SU003",MathScore=100,EnglishScore=100,ChineseScore=88},
                new StudentScore(){StudentName="S0004",MathScore=80,EnglishScore=80,ChineseScore=90}
            };

            //查询所有SU学生
            IEnumerable<StudentScore> studentQuery = from student in students
                                                     where student.StudentName.StartsWith("SU")
                                                     select student;
            //打印学生
            foreach (StudentScore s in studentQuery)
            {
                Console.WriteLine(s.StudentName);
            }


        }
复制代码

 

posted @   echo-efun  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示