我有下面的代码提供搜索,4字段, 对于包含在内的每个字段,lambda会增加很多,而且进行这样的组合搜索是非常耗时且效率不高的,有没有更好的方法来编写正确的搜索?
var result = db.ValuePairs
.Where(c=>
(isStatus && isText && isCode && isGroupCd)
?
c.Status == status && c.Text == text && c.Code == code && c.GroupCode == groupCd
:
(isStatus && isText && isCode)
?
c.Status == status && c.Text == text && c.Code == code
:
(isStatus && isText && isGroupCd)
?
c.Status == status && c.Text == text && c.GroupCode == groupCd
:
(isStatus && isCode && isGroupCd)
?
c.Status == status && c.Code == code && c.GroupCode == groupCd
:
(isText && isCode && isGroupCd)
?
c.Text == text && c.Code == code && c.GroupCode == groupCd
:
(isStatus && isText)
?
c.Status == status && c.Text == text
:
(isStatus && isCode)
?
c.Status == status && c.Code == code
:
(isStatus && isGroupCd)
?
c.Status == status && c.GroupCode == groupCd
:
(isText && isCode)
?
c.Text == text && c.Code == code
:
(isText && isGroupCd)
?
c.Text == text && c.GroupCode == groupCd
:
(isCode && isGroupCd)
?
c.Code == code && c.GroupCode == groupCd
:
(isStatus)
?
c.Status == status
:
(isText)
?
c.Text == text
:
(isCode)
?
c.Code == code
:
(isGroupCd)
?
c.GroupCode == groupCd
:
groupCd != ""
);
答案 0 :(得分:2)
非常简单的布尔逻辑...
.Where(c =>
(!isStatus || c.Status == status)
&& (!isCode || c.Code == code)
&& (!isGroupCd || c.GroupCode == groupCd)
);