简体   繁体   English

C#Linq查询,其中表A列不相等/没有数学表B列联接

[英]C# Linq query where Table A column is not equal/doesnt math Table B column join

I have an issue with ac# linq query where I use the != operator, it works well in SQL but when I write the same query in C# it returns a different result, which is the correct way to the the results where table a column doesn't match table b column. 我在使用!=运算符的ac#linq查询中遇到问题,它在SQL中运行良好,但是当我在C#中编写相同的查询时,它会返回不同的结果,这是在表列中显示结果的正确方法与表b列不匹配。 Please see my sql query below and then my c# query. 请在下面查看我的sql查询,然后查看我的c#查询。

SELECT tba.ID,fa.accountnumber,tba.Account_Number,fa.new_legalname,tba.Legal_Name,fa.new_deliverystatusname, fa.new_deliverystatus,tba.Delivery_Charge
      FROM [CRM_Embrace_Integration].[dbo].[CRM_Tarsus_Debtors_Accounts] tba
      inner join CRM_MBT_GROUP.dbo.FilteredAccount fa
      ON fa.accountnumber collate database_default   = tba.Account_Number
      where fa.new_legalname collate database_default != tba.Legal_Name

and the Linq query looks like this 和Linq查询看起来像这样

var sqlJoinQuery = from accCRM in todaysCRMAccounts
                                   join accSQL in todaysCRMViewAccounts
                                   on accCRM.Account_Number equals accSQL.accountnumber
                                   where accCRM.Legal_Name != accSQL.new_legalname
                                   select new { accCRM.Legal_Name, accSQL.new_legalname };

The SQL query returns the correct result as I want where legal_name(table A) is not equals to legal_name(table B) is the other table. SQL查询返回正确的结果,因为我希望其中legal_name(表A)不等于legal_name(表B)是另一个表。

The Linq query return incorrect result, please assist. Linq查询返回错误结果,请协助。

I suggest to try the following Linq as you want the data that aren't in table 2: 我建议尝试以下Linq,以获取表2中未包含的数据:

var result1 = (from m in db1.Table1
               select m).ToList();

var result2 = (from m in db2.Table2
               select m).ToList(); 

var finalResult = (from m in result1
                   where !(from k in result2
                   select k.Id).Contains(m.Id)
                   select m).ToList();

The above will return that aren't in Table2. 以上将返回Table2中没有的内容。 I hope, this is what you wanted. 希望这就是您想要的。

Your SQL shows you asking for where they are equal. 您的SQL会显示您要求它们相等的地方。 The LINQ shows you asking for when they are not equal. LINQ显示您询问何时不相等。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM