2017年3月8日 星期三

2017年2月24日 星期五

【Entity Framework】Using WHERE IN Clause in Stored Procedure by Entity Framework

這題的問題在 EF ObjectContext 的 YourDbEntities 幫你匯進來的方法參數部分是根據宣告內容哈死的,比方說
Create Proc usp_SelectSomething 
@Column1 int,
@Column2 varchar(255)
As 
    Select * From Table 
    Where Column1 = @Column1 AND Column2 = @Column2
Go
這樣的話在C# EF 取就會是
public ObjectResult sp_SelectSomething(global::System.Int32 column1, global::System.String column2)
{
    ...
}
_MyDbEntitie.usp_SelectSomething(column1, column2);
但是這樣就沒辦法應對 WHERE col IN(...) 的應用,因為無法動態產生不定數量的參數。

2017年2月23日 星期四

【C#】PredicateBuilder usage tip

動態串接 AND, OR 條件應該算是非常常見的需求,在 Linq 段我們常會用 PredicateBuilder 這個擴充來達成,但這個擴充使用上我覺得不是那麼直觀,這篇主要只是要簡單紀錄下
var predicateA = PredicateBuilder.False<YourClass>();
var predicateB = PredicateBuilder.True<YourClass>();
的差異

【C#】Exception when trying Where a PredicateBuilder predicate

Cannot convert expression type 'System.Linq.Expressions.Expression>' to return type 'System.Func'

無法從 'System.Linq.Expressions.Expression>' 轉換為 'System.Func'