1

I have queryparams dto public string[] queryname {get;set;};

  1. if queryname="a" query all name contains "a" data id like 1,4
  2. if queryname="b","c" query all name contains "b,c" data id like 4,5

but var list = db.user.Where(p=>queryname.Contains(p.name));

Now i pass queryname="b","c" query id like 2,3,4,5 ,i hope query id like 4,5 data list ,i want linq to sql code how to coding.

|  user table  |       
|  id  | name  |
|  --  | --    |
| 1    | a     |
| 2    | b     |
| 3    | c     |
| 4    | a,b,c |
| 5    | b,c,d |
1
  • i hope db.user.All(x => x.name.Contains(queryname))****db.user.Any(x => x.name.Contains(queryname)) Commented Mar 13, 2020 at 0:42

1 Answer 1

1

Demo on dotnet fiddle

You can use Intersect() to achieve it.

Produces the set intersection of two sequences.

    var queryname = "b,c";
    var data = new []
    {
        new User { UserId = 1, name = "a" },
        new User { UserId = 2, name = "b" },
        new User { UserId = 3, name = "c" },
        new User { UserId = 4, name = "a,b,c" },
        new User { UserId = 5, name = "b,c,d" }

    };      

    var paramArray = queryname.Split(',');
    var result  = data.Where(p => paramArray.Intersect(p.name.Split(',')).Count() == paramArray.Count());
    foreach(var item in result)
        Console.WriteLine(item.UserId);

Output

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

4 Comments

may be, you can put queryname.Split(',') in variable and use it.
Yeah, I've updated my answer, please take a look at.
yeah it's for solution and factorization ;)
@Phong p.name.Split(',') throw error LINQ to Entities does not recognize the method "System.String [] Split (Char [])", so it cannot be converted to a stored expression. my code is linq to sql

Your Answer

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