Reputation: 2207
I am trying to do a linq to entities statement and one of the conditions is going to contain the wild card %
I have the following code which if i remove the % from the 2 combo boxes i get results
var SLogged = from p in OEEEntity.Scrappages
where p.Date >= StartDate
where p.Date <= EndDate
where p.LogType.Contains(cmbType.Text)
where p.ScrapCode.Contains(cmbCode.Text)
orderby p.Date ascending
select p;
The values that will contain the % are cmbType and cmbCode
anyone know how i can do this.
Upvotes: 0
Views: 1898
Reputation: 15772
This is not answering the QUESTION, since the OP didn't actually ask his problem in the OP.
var queryText = cmbType.Text;
var SLogged = from p in OEEEntity.Scrappages
where p.Date >= StartDate
where p.Date <= EndDate
where p.LogType.Contains(queryText)
|| string.IsNullOrEmpty(queryText)
where p.ScrapCode.Contains(queryText)
|| string.IsNullOrEmpty(queryText)
orderby p.Date ascending
select p;
Upvotes: 0
Reputation: 197
Best way of dealing with wildcard characters is to use PatIndex
var SLogged = (from p in OEEEntity.Scrappages
where p.Date >= StartDate
&& p.Date <= EndDate
&& SqlFunctions.PatIndex(cmbType.Text, p.LogType) > 0
&& SqlFunctions.PatIndex(cmbCode.Text,p.ScrapCode) > 0
orderby p.Date ascending
select p).ToList();
Upvotes: 0
Reputation: 13399
.Contains("aaa")
is equivalent to LIKE '%aaa%'
.StartsWith("aaa")
is equivalent to LIKE 'aaa%'
.EndsWith("aaa")
is equivalent to LIKE '%aaa'
So you don't have to add '%' manually.
Upvotes: 3