3
爲什麼下面的LINQ to SQL語句會引發異常?爲什麼我不能在LINQ to SQL中重用函數
我有一個函數
bool TrimAndCompare(string s1, string s2)
{
return customer.CustomerID.Trim() == customerID.Trim()
}
...一些其他的功能,我所說的以上功能在我的LINQ聲明
var customers = from customer in _context.Customers
where
TrimAndCompare(customer.CustomerID, customerID)
select customer;
以上的LINQ to SQL statment函數拋出一個異常 但下面不是爲什麼?
var customers = from customer in _context.Customers
where
customer.CustomerID.Trim() == customerID.Trim()
select customer;
我得到的「System.NotSupportedException」 ,我嘗試訪問客戶
好吧,如果我想要做的東西,是當我試圖讓一個布爾值回來,但什麼好的。像 變種客戶從客戶=在_context.Customers 其中 customer.GetID()==「一」 選擇顧客; – soldieraman 2010-01-21 08:07:29
@soldierman:該點是一個'Where'表達式* *不返回一個布爾值你是compo se作爲查詢的一部分 - 注意我所有的例子都調用了Where。重點是它創建一個表達式樹,其中包含正確的邏輯,以便您可以將該表達式樹包含在查詢的其餘部分中。 – 2010-01-21 08:17:09