【问题标题】:Join one Table to the Same Table for MULTIPLE Times in Lambda在 Lambda 中将一个表连接到同一个表中多次
【发布时间】:2013-11-24 19:34:22
【问题描述】:

我的 MVC4 应用程序中有一个学生实体和一个城市实体,学生表中有 3 个城市代码,如下所示:

学生实体:

StudentID | StudentName   | BirthCity | LivingCity | UniversityCity |
1           Christof        01           02            03
...


城市实体:

CityID | CityName | 
01       NewYork
02       Los Angeles
03       Washington
...


我想将 Student 实体加入 City 实体,并根据上面所示的值显示 3 个不同的城市名称。我尝试了一些示例,但它们与加入不同的表有关(我需要将同一个表聚合 3 次:City)。我该怎么做?

【问题讨论】:

    标签: sql asp.net-mvc join lambda inner-join


    【解决方案1】:

    我认为使用 join 不是一个好主意。见下文:

    型号:

    public class Student
    {
        public Student()
        {
            StudentID = 0;
            StudentName = string.Empty;
            BirthCity = string.Empty;
            LivingCity = string.Empty;
            UniversityCity = string.Empty;
        }
    
        public Student(int id ,string name, string bName,string lName,string uName)
        {
            StudentID = id;
            StudentName = name;
            BirthCity = bName;
            LivingCity = lName;
            UniversityCity = uName;           
        }
    
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public string BirthCity { get; set; }
        public string LivingCity { get; set; }
        public string UniversityCity { get; set; }
    }
    

    查询:

     Students.Select (
                  s => new  Student()
                     {
                        StudentID = s.StudentID, 
                        StudentName = s.StudentName, 
                        BirthCity = Cities
                           .Where (c => (c.CityID == s.BirthCity))
                           .Select (c => c.CityName)
                           .SingleOrDefault (), 
                        LivingCity = Cities
                           .Where (c => (c.CityID == s.LivingCity))
                           .Select (c => c.CityName)
                           .SingleOrDefault (), 
                        UniversityCity = Cities
                           .Where (c => (c.CityID == s.UniversityCity))
                           .Select (c => c.CityName)
                           .SingleOrDefault ()
                     }
               )
    

    【讨论】:

    • 我出错了。我认为“StudentID”和“StudentName”将是 CityID 和 CityName。但是当我这样使用时,我得到一个错误,比如“AnonymousType#1 不包含'CityID' 的定义,并且找不到接受'AnonymousType#1' 类型的第一个参数的扩展方法'CityID'(你错过了一个使用指令还是程序集引用?”。
    • 您收到 AnonymousType 错误,因为我没有定义选择类型。因为您需要为返回列表定义一个新模型。让我更新,它会更清楚。
    • 对不起,但这没有任何意义。另一方面,我可以设法将类似的代码与 lambda Join 与 2 个表一起使用(无需使用 Viewmodel)。那么,是否可以修改您的代码以便以相同的方式使用?谢谢。
    • 抱歉,没有用。我尝试不使用 viewModel。我再次更新了它,希望它能给你另一个想法。
    • 感谢您的帮助。它很有用,但我认为无需在模型或实体方面进行任何修改。我们只需要修改 lambda 表达式。不过非常感谢。
    【解决方案2】:

    您可以使用子查询来做到这一点。也可以通过连接来完成。

    SELECT StudentId,
        (SELECT CityName FROM City WHERE CityId = s.BirthCity),
        (SELECT CityName FROM City WHERE CityId = s.LivingCity),
        (SELECT CityName FROM City WHERE CityId = s.UniversityCity)
    FROM Student s
    

    【讨论】:

    • 抱歉,我需要一个 LAMBDA 表达式示例而不是 SQL。
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2018-12-19
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多