Linq, Join by comparing with Like

3

I have a question regarding Linq. As I have observed some codes, they usually use for example:

 var _s = (from p in exemplo1
           join q in exemplo2 on p.blabla equals q.blablabla

I would like to know if a structure like for example that I am going to show below is valid and how can I create it:

var _s = (from p in exemplo1
          join q in exemplo2 on SqlMethods.Like(p.blabla, "%" + q.blablabla + "%")
    
asked by anonymous 10.03.2016 / 18:25

2 answers

1

After a few minutes of kicking, I used this solution and it worked.

var _s = (from p in exemplo1
          from q in exemplo2
          where 
          SqlMethods.Like(p.blabla, "%" + q.blablabla + "%")
    
10.03.2016 / 18:38
0

You can use the Contains method to do this.

using (var DBCtx = new RohrdbContext())
{

    var s = (from p in DBCtx.exemplo1
            from q in DBCtx.exemplo2
            where q.blablablabla.Contains(p.blabla)
            select p);

    // eu particulamente prefiro dessa forma.

    var s2 = DBCtx.exemplo1
        .Select(p => new
        {
            P = p,
            C = DBCtx.exemplo2.FirstOrDefault(C => C.Id = p.Id),

        })
        .Where(p => p.P.blablablabla.Contains(p.C.blabla)); 
        //.Where(p => p.P.blablablabla.StartsWith(p.C.blabla)); // Inicio da string
        //.Where(p => p.P.blablablabla.EndsWith(p.C.blabla)); // fim da string
}
    
24.03.2016 / 15:13