0

If I search within a description field using Linq I receive different results when searching with these two queries. First by this %MODULE%ALLEN BRADLEY% and then second by this query %ALLEN BRADLEY%MODULE%. I would like these search queries to give me a result where these words appear in any place of the description field.

I read about sql that using LIKE searches for pattern in string in a correct order, for instance searching for %MODULE%ALLEN BRADLEY% would force the result to be in that particular order within the string. However using the keyword "In" searches for the wildcards anywhere within the string.

How would I achieve that in Linq?

This is my Linq method:

    public List<Item> SearchItems(string itemid, string description)
    {
        return (from allitems in _dbc.Items 
                where SqlMethods.Like(allitems.Number, itemid)
                && SqlMethods.Like(allitems.DESCRIPTION, description)
                select allitems);
    }
4

1 回答 1

1

您可以在多于一行中创建查询,然后使用ToList()以下方法执行它:

public List<Item> SearchItems(string itemid, string description)
{
    IQueryable<Item> query = _dbc.Items.Where(x => SqlMethods.Like(x.Number, itemid));

    foreach(var word in description.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries)
    {
        query = query.Where(x => SqlMethods.Like(x.Description,
                                                 string.format("%{0}%", word)));
    }

    return query.ToList();
}

但是,正如已经建议的那样 - 您应该考虑改用全文搜索。

于 2013-08-19T17:28:53.533 回答