简体   繁体   English

如何在实体框架中使用 <> 表达式

[英]How to use <> expression in Entity Framework

We need to convert following query to Entity framework syntax based query but not fond any alternative for '<>' condition:我们需要将以下查询转换为基于实体框架语法的查询,但不喜欢 '<>' 条件的任何替代方案:

Query:询问:

        select (FirstName+' '+LastName) AS Name ,WorksNumber from [TSHumanResource] 
    join [TSUserProfile] on [TSUserProfile].[TSPersonID] = [TSHumanResource].[TSPersonID] 
    join [TSPerson] on [TSPerson].[TSPersonID] = [TSHumanResource].[TSPersonID] 
    where [TSUserProfile].[TSUserStatusID] = 1 
and [EmployeeReference] <> ' ' and [MMSUserID] is not null order by [WorksNumber] asc

Here's what I was tring:这是我正在尝试的内容:

        (from HR in oDB.TSHumanResources
         join UP in oDB.TSUserProfiles on HR.TSPersonID equals UP.TSPersonID
         join P in oDB.TSPersons on HR.TSPersonID equals P.TSPersonID
         where UP.TSUserStatusID == 1 && HR.EmployeeReference <>
         select new
         {
             ID = e.TSPersonID ,
             ID = e.TID,
         }).Take(10);

The SQL Server operator <> means not equal . SQL Server 运算符<>表示不等于 In c#, the not equal operator is written like this: != .在 C# 中,不等于运算符的写法如下: !=

You have another problem in your linq query - you are currently doing an inner join instead of a left join .您的 linq 查询中还有另一个问题 - 您目前正在执行内部联接而不是左联接

A left join in LINQ is a bit cumbersome comparing to a left join in SQL - it has to go through a group join first.与 SQL 中的左连接相比,LINQ 中的左连接有点麻烦——它必须先通过组连接。 Your query should look more like this:您的查询应该更像这样:

from hr in TSHumanResource
join up in TSUserProfile on hr.TSPersonID equals up.TSPersonID into upgroup
from upg in upgroup.DefaultIfEmpty()
join p in TSPerson on upg.TSPersonID equals p.TSPersonID into pgroup
from puphr in pgroup.DefaultIfEmpty()
where up.TSUserStatusID = 1 
&& HR.EmployeeReference != " " // Assuming you want it different than a single space
// Other conditions here - I don't know where MMSUserID belongs to
order by hr.WorksNumber // just guessing here - I don't know if it's from hr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Linq to Entity Framework的表达式中使用Func? - How to use a Func in an expression with Linq to Entity Framework? 创建用于 Entity Framework 6 的表达式 - Creating an expression for use with Entity Framework 6 如何在实体框架lamba表达式中使用Like运算符 - How to use Like operator in entity framework lamba expression 如何在Asp MVC实体框架中使用使用lambda表达式的连接? - How to use join using lambda expression in Asp MVC Entity Framework? 如何在高性能的实体框架上使用lambda表达式 - how use lambda expression on entity framework with high performance 如何在运行时创建表达式以在具有实体框架的GroupBy()中使用? - How to create an expression at runtime for use in GroupBy() with Entity Framework? 如何在Entity Framework select子句中为值使用动态表达式 - How to use a dynamic expression for a value in an Entity Framework select clause 如何&#39;不&#39;实体框架的lambda表达式 - how to 'not' a lambda expression for entity framework 如何在实体框架的另一个 C# 表达式中使用一个 C# 表达式? - How to use one C# expression inside another C# expression for Entity Framework? 如何在实体框架中使用 lambda 表达式进行此查询? - How to make this query with lambda expression in Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM