使用PredicateExtensions创建sqlscriptes的一个问题

发布于 2024-11-18 11:46:34 字数 1435 浏览 3 评论 0 原文

这是我的逻辑代码:

var predicate = PredicateExtensions.False<Customer>();
predicate = predicate.Or(p => p.CustomerID.Contains("N"));

using (Entities e = new Entities())
{
    var r = from p in e.Customer.AsQueryable().Where(predicate.Compile())
            select p;
    foreach (var _r in r)
        Console.WriteLine(_r.CustomerID);
}

然后我使用sql server profiler获取这样的sql脚本:

SELECT 
[Extent1].[CustomerID] AS [CustomerID], 
[Extent1].[Custname] AS [CustEname], 
[Extent1].[Site] AS [DefaultSite], 
[Extent1].[Currency] AS [Currency], 
[Extent1].[PayTerm] AS [PayTerm], 
[Extent1].[BAddr1] AS [BillAddr1], 
[Extent1].[BAddr2] AS [BillAddr2], 
[Extent1].[BAddr3] AS [BillAddr3], 
[Extent1].[BContact] AS [BillContact], 
[Extent1].[BTel] AS [BillTel], 
[Extent1].[BFax] AS [BillFax], 
[Extent1].[BEMail] AS [BillEMail], 
[Extent1].[Company] AS [DlvyCompany], 
[Extent1].[Addr1] AS [DlvyAddr1], 
[Extent1].[Addr2] AS [DlvyAddr2], 
[Extent1].[Addr3] AS [DlvyAddr3], 
[Extent1].[Contact] AS [DlvyContact], 
[Extent1].[Tel] AS [DlvyTel], 
[Extent1].[Fax] AS [DlvyFax], 
[Extent1].[EMail] AS [DlvyEMail], 
[Extent1].[Remark] AS [Remark], 
[Extent1].[User] AS [User], 
[Extent1].[DT] AS [UpdateDT]
FROM [dbo].[Customer] AS [Extent1]

我从数据库中获取所有数据,但我希望sql脚本末尾有一个条件,意味着获取所有数据但customeid包含N(p => p.CustomerID.Contains("N"))

,我得到了一些错误...... 请给一些积分。谢谢!

here is my logic code:

var predicate = PredicateExtensions.False<Customer>();
predicate = predicate.Or(p => p.CustomerID.Contains("N"));

using (Entities e = new Entities())
{
    var r = from p in e.Customer.AsQueryable().Where(predicate.Compile())
            select p;
    foreach (var _r in r)
        Console.WriteLine(_r.CustomerID);
}

and then i use sql server profiler get the sql scripts like this:

SELECT 
[Extent1].[CustomerID] AS [CustomerID], 
[Extent1].[Custname] AS [CustEname], 
[Extent1].[Site] AS [DefaultSite], 
[Extent1].[Currency] AS [Currency], 
[Extent1].[PayTerm] AS [PayTerm], 
[Extent1].[BAddr1] AS [BillAddr1], 
[Extent1].[BAddr2] AS [BillAddr2], 
[Extent1].[BAddr3] AS [BillAddr3], 
[Extent1].[BContact] AS [BillContact], 
[Extent1].[BTel] AS [BillTel], 
[Extent1].[BFax] AS [BillFax], 
[Extent1].[BEMail] AS [BillEMail], 
[Extent1].[Company] AS [DlvyCompany], 
[Extent1].[Addr1] AS [DlvyAddr1], 
[Extent1].[Addr2] AS [DlvyAddr2], 
[Extent1].[Addr3] AS [DlvyAddr3], 
[Extent1].[Contact] AS [DlvyContact], 
[Extent1].[Tel] AS [DlvyTel], 
[Extent1].[Fax] AS [DlvyFax], 
[Extent1].[EMail] AS [DlvyEMail], 
[Extent1].[Remark] AS [Remark], 
[Extent1].[User] AS [User], 
[Extent1].[DT] AS [UpdateDT]
FROM [dbo].[Customer] AS [Extent1]

that i get all the data from my database, but i hope there is a condition at the end of the sql scripts, means get all the data but customeid contains N(p => p.CustomerID.Contains("N"))

where i get some wrong....
pls give some points.thans!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2024-11-25 11:46:34

问题出在 Compile() 方法上。您想要使用的 IQueryable 扩展方法需要表达式作为其参数。在您的情况下,调用 AsQueryable() 不会执行任何操作,并且 IEnumerable 扩展方法 Where(Func)被叫。您想要做的是调用 IQueryable 扩展方法 Where(Expression>) 来代替。要做到这一点很简单:只需不要调用 Compile()predicate 已经是一个表达式。另外,您使用查询表达式 (from ...) 的方式(几乎)不会执行任何操作,因此您可以将其省略:

var r = e.Customer.Where(predicate);

The problem lies in the Compile() method. The IQueryable<T> extension methods that you want to use require expressions as their arguments. Calling AsQueryable() doesn't do anything in your case and the IEnumerable<Customer> extension method Where(Func<Customer, bool>) gets called. What you want to do is to call the IQueryable<Customer> extension method Where(Expression<Func<Customer, bool>>) instead. To do that is simple: just don't call Compile(), predicate is already an expression. Also, the way you use the query expression (from …) doesn't do (almost) anything, so you can leave it out:

var r = e.Customer.Where(predicate);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文