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;
开发者_StackOverflow社区 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!!!
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);
精彩评论