c# - 过滤列表 B 中但不在 A 中的实体
问题描述
我有一个名为 的数据库表Autoline_PL_00_trans
,它代表收到的信用票据。信用票据必须手动输入到我们的经销商管理系统中,然后在我的应用程序中标记为“已预订”。在应用程序的一部分中,我想从整个贷方通知单列表中展示尚未标记为“已预订”的那些。如果它们被标记为已预订,我只需将它们写回FundsBooked
桌子。我无法将任何数据存储到Autoline_PL_00_trans
表中,因为那是我拥有只读访问权限的经销商管理系统的数据库。两个表都来自两个不同的数据库,这使得使用单个 dbcontext 成为不可能。这是我可用的属性的结构:
public class Autoline_PL_00_trans
{
public string CreditNoteNumber {get; set;}
public decimal Amount {get; set;}
public DateTime ReceivedDate {get; set;}
}
public class FundsBooked
{
public string CreditNoteNumber {get; set;}
public bool IsBooked {get; set;}
}
allFunds = alContext.Autoline_PL_00_trans.ToList(); fundBooked = gwContext.FundsBooked.ToList();
我现在需要的是所有尚未预订的资金,因此基于 CreditNoteNumber 作为键的 allFundings 中但不在 fundBooked 中的实体。在我的测试数据中,我有一个 FundsBooked,它也在 Autoline_PL_00_trans 中表示。这是我试图想出的,但我认为这不起作用
本机 linq/efcore 方法:
var notBooked allFunds.Select(x => x.CreditNoteNumber ).Except(fundsBooked.Select(y => y.CreditNoteNumber )).ToList();
--> 空结果集
原始 SQL 方法:
在我的 MS SQL 工作台中工作的原始 sql 语句将是:
SELECT
[SupplierReference]
FROM [Autoline_Basis].[dbo].[Autoline_PL_00_trans] t1
Where NOT EXISTS (
select [SupplierReference]
from [LUX_WEB_Goodwill].[dbo].[FundsBooked] t2
Where t1.SupplierReference = t2.SupplierReference
)
当我尝试将其翻译为 c# 时,它看起来像这样:
var notBooked = gwContext.FundsBooked.FromSqlRaw("SELECT [SupplierReference] FROM [Autoline_Basis].[dbo].[Autoline_PL_00_trans] t1 " +
"Where NOT EXISTS (select [SupplierReference] from [LUX_WEB_Goodwill].[dbo].[FundsBooked] t2" +
"Where t1.SupplierReference = t2.SupplierReference)")
.ToList();
但我收到以下错误:
Microsoft.Data.SqlClient.SqlException:''t1' 附近的语法不正确。'
编辑:好的伙计们,我整理了我的问题,终于找到了可行的解决方案:
var notBooked = alContext.AutolinePl00Trans.FromSqlRaw("SELECT [SupplierReference], [DocumentDate], [DocumentBaseTotal] FROM [Autoline_Basis].[dbo].[Autoline_PL_00_trans] t1 " +
"Where NOT EXISTS (select [SupplierReference] from [LUX_WEB_Goodwill].[dbo].[FundsBooked] t2 " +
"Where t1.SupplierReference = t2.SupplierReference) AND t1.SupplierReference LIKE '306%N%'").Select(x => new { x.SupplierReference, x.DocumentDate, x.DocumentBaseTotal})
.ToList();
解决方案
推荐阅读
- javascript - 为什么我有一个 document.getElementById 为 Null?
- c++ - 如何在 C++ 中为具有 2 个变量的对象重载增量运算符?
- r - 如何在插入符号中从此模型中提取系数?
- optimization - C++CLI 优化了吗?
- c - 在 C,Visual Studio 中释放内存时出错
- loops - 批量迭代 MSBuild 输出 - MSB1008:只能指定一个项目
- c++ - How to synchronize two RichEdit's scrolling position?
- alexa - 从 Alexa 应用程序中的自定义意图调用其他意图是否合乎逻辑
- c# - 寻找一种更简单的方法来写这个。带有插入和修剪的子字符串
- memory - 根据 pmap,JVM 总是在我的 linux 服务器中保留近 1G 内存