如何翻译

select *from
   (
     select EmployeeID,FirstName,LastName,Region from Employees where city
     in ('London','Seattle')
   )
   x
where x.Region is not null


转换为Linq等效项。

我尝试过(但是也会选择空值)

LinqDBDataContext Context = new LinqDBDataContext();

   var query = from emps in

                  (
                       from empls in Context.Employees
                       where empls.City == "London" || empls.City == "Seattle"
                        && empls.Region != null
                             select new
                              {
                                 FirstName = empls.FirstName,
                                 LastName = empls.LastName,
                                 City = empls.City,
                                 Region = empls.Region
                              }
                   )
              select emps;


            GridView1.DataSource = query;
            GridView1.DataBind();

最佳答案

这是你的括号。您在城市的OR语句中缺少它们。

LinqDBDataContext Context = new LinqDBDataContext();

   var query = from emps in

                  (
                       from empls in Context.Employees
                       where (empls.City == "London" || empls.City == "Seattle")
                        && empls.Region != null
                             select new
                              {
                                 FirstName = empls.FirstName,
                                 LastName = empls.LastName,
                                 City = empls.City,
                                 Region = empls.Region
                              }
                   )
              select emps;


            GridView1.DataSource = query;
            GridView1.DataBind();

关于c# - 将Sql转换为Linq,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1887761/

10-13 02:24