Reputation: 24498
I am trying to search within a string for a specific value.
What I want to do is SPLIT the string and look at each value. If the mySearch.EQUALS(split-string-value) then return the record(s) in a ToList().
This is my code.. It returns ZERO RECORDS
// user search for sku = "2012"
// p.productMatch in DB = "2012,2012L,2012LR,2011"
var result = ctx.Products
.Where(p => p.clientID == Config.ClientID)
.Where(p => p.productMatch.Contains(sku))
.Select(v => new ProductView
{
productID = v.productID,
productMatch = v.productMatch,
productSKU = v.productSKU
}).ToList();
var results = from w in result
where w.productMatch.Split(',').Equals(sku)
select w;
return results.ToList();
Upvotes: 1
Views: 138
Reputation: 54927
In your code, you’re performing an equality comparison on the string[]
array produced by the Split
operation and the sku
variable. Since the two are of different types, the equality comparison will never succeed.
What you actually need to do is compare each element of your string[]
arrays with sku
:
var results =
from w in result
from part in w.productMatch.Split(',')
where part.Equals(sku)
select part;
If you want to return the entire record, use:
var results =
from w in result
where w.productMatch.Split(',').Any(part => part.Equals(sku))
select w;
Or, better yet, use the Contains
method, as suggested by @jimmy_keen, for more concise code.
Upvotes: 2
Reputation: 31484
At the moment, in your where w.productMatch.Split(',').Equals(sku)
line you compare result of Split()
(which is array of strings) with single string sku
. For obvious reasons this fails. Simply change it to:
where w.productMatch.Split(',').Contains(sku)
Upvotes: 4