.net 如何作为LINQ查询的一部分执行代码

.net 如何作为LINQ查询的一部分执行代码,.net,asp.net-mvc,linq,linq-to-entities,.net,Asp.net Mvc,Linq,Linq To Entities,我有一个如下的查询: var results = from person where <here I need to do something like if person is of type Employee, call person.GetSalary() > 100000 but if the person is of type Contractor, I need to execute several lines of cod

我有一个如下的查询:

    var results = from person
                  where <here I need to do something like if person is of type 
Employee, call person.GetSalary() > 100000 but if the person is of type Contractor, I need to execute 
several lines of code before doing a person.GetSalary() > 100000 
                  select new {person.Name}
var结果=来自个人
其中100000,但如果该人员为承包商类型,则我需要执行
执行person.GetSalary()>100000之前的几行代码
选择新的{person.Name}

难点在于构造where子句。有人能帮我完成这个查询吗?

您可以编写一个方法来执行逻辑检查,并在单独的where子句中调用它。要使用LINQ To实体执行此操作,必须首先使用
AsEnumerable()
具体化结果:

现在你可以写:

var results = from person in (
                  (from p in person select new { p.Name } ).AsEnumerable())
              where CheckEmployeeOrContractorSalary( person, 100000 )
              select new {person.Name};

问题被标记为“Linq到实体”;自定义方法无法转换为SQL,因此如果您这样做,您需要在本地筛选结果(通过在Where之前调用AsEnumerable)@Thomas-您能否详细说明此解决方案不起作用的原因,是的,它是Linq to Entities。另一个问题是-如何调试Linq查询?假设我有一个很长的linq查询,其中有一堆where、join和let,我需要在其中一行上设置断点来检查值。这似乎是不可能的。@DotnetDude,Linq to Entities查询被转换为SQL并在数据库上执行。因此,您只能使用由查询生成器识别的构造和方法,当然CheckEmployeeOrContractorSalary方法不是这样。关于调试,您不能将断点放在Linq查询中,因为正如我所说的,它不是在本地执行的,而是在database@Thomas你说得对。我没看到那个标签。我将编辑我的回复@调试LINQ可能有点棘手。您仍然可以在LINQ to objects查询中的任何方法或表达式中放置断点,但要意识到,只有在开始枚举结果时才会碰到这些断点。在LINQ查询中调试连接和选择之类的东西实际上是不可能的,因为实现隐藏在可枚举类的行为中。在可能的情况下,一种方法是从一个简单的有效查询构建一个复杂的查询。
var results = from person in (
                  (from p in person select new { p.Name } ).AsEnumerable())
              where CheckEmployeeOrContractorSalary( person, 100000 )
              select new {person.Name};