I have TableA
with 3 columns ColA, ColB, ColC
. I'm developing a query function like the code shown below, because I want to make that if ValueB
& ValueC
are null, or string.Empty
, then ColB, ColC
may be not included in the query condition.....
IList<TableA> Query(object ValueA,string ValueB,string ValueC)
var result=(from TableA TA in entity.TableA
where TA.ColA==ValueA
&&(string.IsNullOrWhiteSpace(ValueB)?1==1:TA.ColB==ValueB)
&&(string.IsNullOrWhiteSpace(ValueC)?1==1:TA.ColC==ValueC)
select TA);
but I got a "System.NotSupportedException" exception while executed to this part:
string.IsNullOrWhiteSpace(ValueB)
Could someone guide me a right way to make it?
Sorry for being a Taiwanese so the error message I met contains Chinese words.
CodePudding user response:
Compose your query conditionally. Start with the base query:
var query = entity.TableA
.Where(ta => ta.ColA == ValueA);
Then build upon the query when the other parameter values are not null or white space:
if(!string.IsNullOrWhitespace(ValueB))
{
query = query.Where(ta => ta.ColB == ValueB);
}
CodePudding user response:
Instead of using string.IsNullOrWhiteSpace method. You can check like below
(ValueB == null || ValueB == "")