Accessing transparent identifier in select throws it is not referenced from scope

Viewed 219

Accessing transparent identifier in select throwing exception.

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.

1 Answers

Need to create a class with your result.

Please check the following way.

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; }
            }
Related