C # search for matching words in a table column using Linq2Sql

I am trying to use Linq2Sql to return all rows containing values ​​from a list of rows. The linq2sql class object has a string property that contains words separated by spaces.

public class MyObject
{
    public string MyProperty { get; set; }
}

Examples of MyProperty values:

MyObject1.MyProperty = "text1 text2 text3 text4"
MyObject2.MyProperty = "text2"

For example, using a collection of strings, I pass the list below

var list = new List<>() { "text2", "text4" }

This will return both elements in my example above, since both of them contain the value "text2".

I tried using the following code, however, due to my extension method, Linq2Sql could not be evaluated.

public static IQueryable<MyObject> WithProperty(this IQueryable<MyProperty> qry,
    IList<string> p)
{
    return from t in qry
        where t.MyProperty.Contains(p, ' ')
        select t;
}

I also wrote an extension method

public static bool Contains(this string str, IList<string> list, char seperator)
{
    if (str == null) return false;
    if (list == null) return true;

    var splitStr = str.Split(new char[] { seperator },
        StringSplitOptions.RemoveEmptyEntries);

    bool retval = false;
    int matches = 0;

    foreach (string s in splitStr)
    {
        foreach (string l in list)
        {
            if (String.Compare(s, l, true) == 0)
            {
                retval = true;
                matches++;
            }
        }
    }

    return retval && (splitStr.Length > 0) && (list.Count == matches);
 }

Any help or ideas on how I could achieve this?

+3
source share
2 answers

. WithProperty IQueryable<MyObject>, IQueryable<MyProperty>.

IQueryable. Contains lambda . :

List<string> searchStrs = new List<string>() { "text2", "text4" }

IEnumerable<MyObject> myFilteredObjects = dataContext.MyObjects
                   .Where(myObj => myObj.MyProperty.Contains(searchStrs, ' '));

Update:

. , Contains SQL. , , " SQL?": , . , Linq-to-SQL . . . :

  • ,
  • seperator

SQL Linq-to-SQL. , . .

public static IEnumerable<MyObject> ContainsOneOfTheseKeywords(
        this IQueryable<MyObject> qry, List<string> keywords, char sep)
{
    List<List<MyObject>> parts = new List<List<MyObject>>();

    foreach (string keyw in keywords)
        parts.Add((
            from obj in qry
            where obj.MyProperty == keyw ||
                  obj.MyProperty.IndexOf(sep + keyw + sep) != -1 ||
                  obj.MyProperty.IndexOf(keyw + sep) >= 0 ||
                  obj.MyProperty.IndexOf(sep + keyw) ==
                      obj.MyProperty.Length - keyw.Length - 1
            select obj).ToList());

    IEnumerable<MyObject> union = null;
    bool first = true;
    foreach (List<MyObject> part in parts)
    {
        if (first)
        {
            union = part;
            first = false;
        }
        else
            union = union.Union(part);
    }

    return union.ToList();
}

:

List<string> searchStrs = new List<string>() { "text2", "text4" };

IEnumerable<MyObject> myFilteredObjects = dataContext.MyObjects
                    .ContainsOneOfTheseKeywords(searchStrs, ' ');

- , . 10 db 10 . . , Linq (, , , ).

. , , .

. ( , , ), . . . , .

+1

, , :

from t in ctx.Table
where list.Any(x => t.MyProperty.Contains(x))
select t

Any() All(), , list

EDIT:

, , , linq, All Any

where list.Any(x => t.MyProperty.Contains(x))

:

where t.MyProperty.Contains(list[0]) || t.MyProperty.Contains(list[1]) ||
      t.MyProperty.Contains(list[n])

where list.Any(x => t.MyProperty.Contains(x))

:

where t.MyProperty.Contains(list[0]) && t.MyProperty.Contains(list[1]) &&
      t.MyProperty.Contains(list[n])
0

Source: https://habr.com/ru/post/1740651/


All Articles