我有一個物體框架查詢,其中 where 子句根據幾個選擇而變化。
這是我的代碼:
Expression<Func<CtCsGroup, bool>> where_expression1 = x => 1 == 1;
Expression<Func<CtCsGroup, bool>> where_expression2 = x => 1 == 1;
Expression<Func<CtCsGroup, bool>> where_expression3 = x => 1 == 1;
if (selections.SearchCSGroup.NoNull() != "")
{
where_expression1 = x => x.CsGroup.ToUpper() == selections.SearchCSGroup.ToUpper();
}
if (selections.SearchUserId.NoNull() != "")
{
where_expression3 = x => x.UsrIdn.ToUpper() == selections.SearchUserId.ToUpper();
}
if (!selections.SearchIncludeRetires)
{
where_expression2 = x => x.CsRetire != "Y";
}
var groupDTOs = (from g in _infobaseContext.CtCsGroup
.Where(where_expression1)
.Where(where_expression2)
.Where(where_expression3)
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = "test" // n.CsGroupName
}).ToList();
我想將連接添加到另一個表。
如何將此添加到上述查詢中?我嘗試了幾種不同的格式,但每當我添加它時,我都會收到錯誤"(where_expression1)"、"(where_expression3)"等。
謝謝。
這里有更多細節......
var groupDTOs = (from g in _infobaseContext.CtCsGroup
join n in _infobaseContext.TblCsGroupList
on g.CsGroup equals n.CsGroup
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = n.CsGroupName
}).ToList();
- 這個沒有加入就可以作業 -
var groupDTOs = (from g in _infobaseContext.CtCsGroup
.Where(where_expression1)
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = "test" //n.CsGroupName
}).ToList();
- 但是當我同時添加連接和運算式時 -
var groupDTOs = (from g in _infobaseContext.CtCsGroup
join n in _infobaseContext.TblCsGroupList
on g.CsGroup equals n.CsGroup
.Where(where_expression1)
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = n.CsGroupName
}).ToList();
- Visual Studio 出現此錯誤:引數 2:無法從 'System.Linq.Expressions<System.Func<IBDataServices.Models.CtCsGroup, bool>>' 轉換為 'System.Func<char,bool>'

uj5u.com熱心網友回復:
通常,我們會努力通過多個步驟組合查詢,而不是嘗試注入多個預先組合的謂詞。您已經在使用Fluent表示法來應用您的謂詞,因此將該程序從基于Query的運算式中分離出來。
您還會注意到比較值已被引數化,而不是讓linq 解釋器決定什么是查詢參考,什么是我們的離散值或標量值。
您尚未發布具體錯誤,但是在評估linq運算式的
elections.SearchCSGroup位置可能超出范圍且無法解決。有意的引數化避免了這種混淆。
IQueryable<CtCsGroup> groupQuery = _infobaseContext.CtCsGroup;
if (selections.SearchCSGroup.NoNull() != "")
{
// Force the comparison against a discrete parameter value
var selection = selections.SearchCSGroup.ToUpper();
groupQuery = groupQuery.Where(x => x.CsGroup.ToUpper() == selection);
}
if (selections.SearchUserId.NoNull() != "")
{
var selection = selections.SearchUserId.ToUpper();
groupQuery = groupQuery.Where(x => x.UsrIdn.ToUpper() == selection);
}
if (!selections.SearchIncludeRetires)
{
// NOTE: "Y" will be correctly interpreted as a discrete parameter value
groupQuery = groupQuery.Where(x => x.CsRetire != "Y");
}
var groupDTOs = (from g in groupQuery
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = "test" // n.CsGroupName
}).ToList();
最后一部分也可以組成一個流利的運算式,而不是一個基于查詢的運算式:
var groupDTOs = groupQuery.Select(g => new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = "test" // n.CsGroupName
}).ToList();
這種模式通過首先嘗試通過預編譯的謂詞來幫助您避免這些問題,特別是它消除了使用不正確的型別系結定義謂詞的可能性。
此模式還有助于組合具有條件排序列或方向的運算式。
您發布的最后一個問題是一個紅鯡魚,您混淆了語法,該運算式從未想過作業。
Visual Studio 出現此錯誤:引數 2:無法從 'System.Linq.Expressions<System.Func<IBDataServices.Models.CtCsGroup, bool>>' 轉換為 'System.Func<char,bool>'
Specifically this section:
join n in _infobaseContext.TblCsGroupList
on g.CsGroup equals n.CsGroup.Where(where_expression1)
You probably see it now that the line break is removed, n.CsGroup is a string, and it itself IEnumerable<char> so it is assuming you want to apply the where_expression1 predicate to the value of n.CsGroup
We can still join onto the composed query using the normal linq join expressions:
var groupDTOs = (from g in groupQuery
join n in _infobaseContext.TblCsGroupList
on g.CsGroup equals n.CsGroup
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = n.CsGroupName
}).ToList();
If your schema has a navigation property on CtCsGroup called CsGroup that represents the relationship between TblCsGroupList and CtCsGroup then we can use dot notation and the necessary joins will be implicit.
NOTE: This makes broad assumptions about the schema and is only included to show a common structural alternative to manually defining joins
var groupDTOs = (from g in groupQuery
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = g.CsGroup.CsGroupName
}).ToList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/422290.html
標籤:
