下面是一个使用LINQ查询实体的示例代码:

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqEntityQueryExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个示例的实体集合
            List<Student> students = new List<Student>()
            {
                new Student { Id = 1, Name = "Tom", Age = 20 },
                new Student { Id = 2, Name = "Jerry", Age = 22 },
                new Student { Id = 3, Name = "Alice", Age = 21 },
                new Student { Id = 4, Name = "Bob", Age = 23 }
            };

            // 使用 LINQ 查询实体
            var query = from student in students
                        where student.Age > 20
                        orderby student.Name
                        select student;

            // 输出查询结果
            foreach (var student in query)
            {
                Console.WriteLine($"Id: {student.Id}, Name: {student.Name}, Age: {student.Age}");
            }
        }
    }

    // 示例的实体类
    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

这个示例中,我们创建了一个名为Student的实体类,包含了Id、Name和Age属性。然后我们创建了一个包含几个示例学生对象的实体集合students。

使用LINQ查询实体的代码如下:

var query = from student in students
            where student.Age > 20
            orderby student.Name
            select student;

这个查询语句选择了年龄大于20的学生对象,并按照姓名进行升序排序。

最后,我们使用foreach循环遍历查询结果,并将每个学生对象的属性值输出到控制台

写一个linq实体查询

原文地址: https://www.cveoy.top/t/topic/iwjv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录