c# - 访问 select 中的透明标识符会引发它不是从范围引用的

标签 c# asp.net-mvc entity-framework linq .net-core

在选择抛出异常时访问透明标识符。

var result = (from t1 in table1
              join t2 in table2 on t1.col1 equals t2.col1 into myTable1
              from a in myTable1.DefaultIfEmpty()
              join t3 in table3 on t1.col2 equals t3.col2 into myTable2
              from b in myTable2.DefaultIfEmpty()
              where t1.col1 == 1
              select new
              {
                   prop1 = b,
                   prop2 = myTable1,
                   prop3 = myTable1.Count()
              });
异常(exception):variable 'myTable1' of type 'System.Collections.Generic.IEnumerable' referenced from scope '', but it is not defined.

最佳答案

需要用你的结果创建一个类。
请检查以下方式。

PropModel propModel = new PropModel();

            propModel  = (from t1 in table1
                          join t2 in table2 on t1.col1 equals t2.col1 into myTable1
                          from a in myTable1.DefaultIfEmpty()
                          join t3 in table3 on t1.col2 equals t3.col2 into myTable2
                          from b in myTable2.DefaultIfEmpty()
                          where t1.col1 == 1
                          select new
                          {
                              prop1 = b,
                              prop2 = myTable1.FirstOrDefault(),
                              prop3 = myTable1.Count()
                          }).FirstOrDefault();

public class PropModel
            {
                public table3 prop1 { get; set; }
                public table2 prop2 { get; set; }
                public Int32 prop3 { get; set; }
            }

关于c# - 访问 select 中的透明标识符会引发它不是从范围引用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69162072/

相关文章:

c# - 在运行时在网格项模板内动态创建服务器控件

asp.net-mvc - .NET MVC的理想文件夹结构

c# - Entity Framework 保存循环中的第一项,但不保存其他项

entity-framework - 使用 MVC2 更新带有外键的 Entity Framework v4 对象失败

c# - Xamarin.Forms iOS依赖服务中的触觉反馈崩溃

c# - WindowsIdentity 构造函数使用 LogonUser 的 token 引发异常

c# - MVC 中的显示与编辑模式

c# - 当有复合主键并且只想使用一个时使用 db.Find

c# - 使用扩展方法 (C# 3.0) 扩展字符串?

asp.net-mvc - 如何在 ASP.NET MVC 3 razor ViewStart 文件中指定不同的布局?