【发布时间】:2022-09-29 00:53:58
【问题描述】:
我有以下两个模型:
public class Transaction
{
public int TransactionId { get; set; }
public string Description { get; set; }
}
public class TransactionRule
{
public int TransactionRuleId { get; set; }
public string Pattern;
public string action;
}
我的数据库中的两个表之间没有正式的关系。但是,TransactionRules 包含列 \"pattern\",其中包含 SQL LIKE 模式,例如 \"Hell World%\"。 Transactions.Descriptions 列与 TransactionRules.pattern 列匹配。这将允许我获得所有交易的列表以及与描述相匹配的任何规则。希望下面的查询更好地描述了这一点。
select
t.*,
tr.pattern,
from dbo.Transactions t
left join dbo.TransactionRules tr ON t.Description LIKE tr.pattern
我可以毫无问题地在 SQL 中使用它,但我正在努力生成一个 LINQ 等价物。我会发布到目前为止我尝试过的内容,但它们都会产生语法错误,因此不会为我的帖子增加任何价值。
虽然我可以求助于使用 SQL,但我真的更愿意在 Linq 中尝试这个,因为它可以帮助我更好地理解 Linq(这是学习 Linq 的练习)。
-
你知道
EF.Functions.Like吗? -
@GertArnold 我尝试对连接中的模式列值使用 EF LIKE 函数,但无法获得正确的语法,这是我需要帮助的地方
-
不要使用join,只使用where。
-
从来没有做过这样的事情,但你可以尝试交叉连接,例如
(from t in Transaction from tr in TransactionRule where EF.Functions.Like(t.Description, tr.Pattern) select new { t.TransactionId , t.Description, tr.Pattern }) -
@sgmoore - 这几乎可以工作,但我需要两者之间的左连接。如果不存在匹配项,它应该像我的问题中的 sql 查询一样返回
标签: c# .net linq entity-framework-core