Join语句中的条件所在的Lambda - c#

Improve this question

我必须根据其Employee过滤department。我可以使用LINQ进行相同的操作。

Linqlambda进行编译以获得相同的结果。编译器在编译之前将查询表达式更改为等效的Lambda expression,因此生成的IL完全相同。Source

var deptCollection = new List<Dept>();
var employeeCollection = new List<Employee>();

employeeCollection.Add(new Employee { Id = 1, Name = "Eldho" });

deptCollection.Add(new Dept { DepetarmentName = "a", EmployeeId = 3 });
deptCollection.Add(new Dept { DepetarmentName = "a", EmployeeId = 1 });

var empinadept = (from e in employeeCollection
                  from dep in deptCollection
                  where e.Id == dep.EmployeeId
                  && dep.DepetarmentName == "a"
                  select e)
                 .ToList();

我无法在此Lambda中添加.Where条款

var empindeptLamda = employeeCollection
                     .Join(deptCollection,
                     emp => emp.Id, dep => dep.EmployeeId,
                     (em, dep) => em.Id == dep.EmployeeId
                      && dep.DepetarmentName == "a")
                     .ToList();

class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Dept
{
    public int EmployeeId { get; set; }
    public string DepetarmentName { get; set; }
}

Q1。上面的linq的等效lambda语句是什么? (如何在方法语法中的linq中添加where子句

参考方案

与此等效:

var empinadept = (from e in employeeCollection
              from dep in deptCollection
              where e.Id == dep.EmployeeId
              && dep.DepetarmentName == "a"
              select e)
             .ToList();

这是:

var result = employeeCollection.Join(deptCollection,
        e => e.Id,
        dep => dep.EmployeeId,
        (e,dep) => new { e, dep })
    .Where(item => item.dep.DepetarmentName == "a")
    .Select(item => item.e)
    .ToList();

更好的选择是:

var result = employeeCollection.Join(
            deptCollection.Where(dep => dep.DepetarmentName == "a"),
            e => e.Id,
            dep => dep.EmployeeId,
            (e,dep) => e)
       .ToList();

最接近查询语法(但我会说基于观点的意见不太好)是:

var result = employeeCollection.Join(
        deptCollection,
        e => new { e.Id, "a" },
        dep => new { dep.EmployeeId, dep.DepartmentName },
        (e,dep) => e).ToList();

Python 3运算符>>打印到文件 - python

我有以下Python代码编写项目的依赖文件。它可以在Python 2.x上正常工作,但是在使用Python 3进行测试时会报告错误。depend = None if not nmake: depend = open(".depend", "a") dependmak = open(".depend.mak&#…

无法解析Json响应 - php

我正在尝试解析一个包含产品类别的JSON文件,然后解析这些类别中的产品,并在div中显示它们。我的问题:虽然我可以获取类别,但我不知道如何索取产品(并将它们显示在类别下)。我的剧本:<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> …

在xpath中选择多个条件 - php

我正在尝试使用来自高尔夫比赛的xml提要,以显示每个高尔夫球手在高尔夫球场上的位置。目前,我想展示符合两个条件的所有高尔夫球手(排在前25名,以及所有加拿大高尔夫球手)。这是xml提要的示例。<GolfDataFeed Type="Leaderboards" Timestamp="3/21/2012 9:18:09 PM&…

与哪些运算符>>兼容 - java

我这里没有什么代码int b=3; b=b >> 1; System.out.println(b); 它可以完美工作,但是当我将变量b更改为byte,short,float,double时,它包含错误,但是对于变量int和long来说,它可以完美工作,为什么它不能与其他变量一起工作? 参考方案 位移位运算符(例如>>)与任何整数类型兼…

使用javascript在客户端的列表视图中选择所有复选框 - javascript

我有一个列表视图,在标题中有一个复选框。如果标题复选框已选中/未选中,我想选择行中的所有复选框。如何在客户端实现此目标?这是ListView设计代码。<asp:ListView ID="lvTypes" runat="server" GroupPlaceholderID="groupPlaceHolde…