3

I am trying to run the below code, and it works properly until I try and add a second search value after using a comma to add a new value that gets added to the array of values. In SQL terms, I basically want to add the "AND" keyword after each loop iteration. Is this somehow possible?

var query = from s in db.Items 
                    join c in db.Categories on s.Category equals c.ID into cats from c in cats.DefaultIfEmpty()
                    join ma in db.Makes on s.Make equals ma.ID into maks from ma in maks.DefaultIfEmpty()
                    join mo in db.Models on s.Model equals mo.ID into mods from mo in mods.DefaultIfEmpty()
                    join st in db.Status on s.Usage_Status equals st.ID into stats from st in stats.DefaultIfEmpty()
                    join d in db.Departments on s.Department equals d.ID into deps from d in deps.DefaultIfEmpty()
                    select new { item = s, cat = c, make = ma, model = mo, status = st, department = d };

if (!String.IsNullOrEmpty(searchString))
{
    string[] searchValues = searchString.Split(',');

    for (var x = 0; x < searchValues.Length; x++)
    {
        var value = searchValues[x];
        query = query.Where(s => s.item.Asset_Tag_Nbr.Contains(value)
                           || s.item.Serial.Contains(value)
                           || s.cat.Name.Contains(value)
                           || s.make.Name.Contains(value)
                           || s.model.Name.Contains(value)
                           || s.item.Assigned_User.Contains(value));
    }
}
10
  • 1
    So assuming the searchString is This, That, you want to get the items where one of the properties contains both This and That? are you sure you want and and not or? BTW, searchValues[x] is already a string, the ToString is redundant. Commented Dec 11, 2018 at 16:32
  • if query is an IQueryable then you're already doing what you're asking. But then you say that's not working properly when you add another search value, so i'm guessing the problem is what @ZoharPeled said Commented Dec 11, 2018 at 16:39
  • I would want it to be an 'and' statement because I want the user to be able to narrow down the results. For example they would type "Desktop, Dell" and find only the results with those matching values. Commented Dec 11, 2018 at 16:40
  • This should work, check what is query. If its IQueryable around correct type it shoudl work, might be that @ZoharPeled pointed out the mistake in your logic Commented Dec 11, 2018 at 16:41
  • I think the issue is more of a linq syntax error. I'm not too experienced with linq, but maybe I can't run "query.Where" more than once? Commented Dec 11, 2018 at 16:48

1 Answer 1

3

It turns out that the code was working as it should, and was just developer error. I was adding a space after the comma when entering search terms. Changing var value = searchValues[x] to var value = searchValues[x].Trim() did the trick and cleared up the empty spaces on the ends.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.