0

I'd like to do a Join but I need to compare more than just a simple key. For example, I'd like to do something like the following:

from a in tableA
join b in tableB on a.x between b.minX and b.maxX

Not very complicated. But the functions for the Join lambda method seem to want a simple key to compare.

Any thoughts?

4
  • 1
    Why not simply use an appropriate where clause? Commented Jan 5, 2012 at 19:47
  • Can you show some sample data and what you'd hope to see? Commented Jan 5, 2012 at 19:48
  • @Tim I don't think there is a appropriate where clause, because I'm trying to Join two tables -- so the values that go into the where clause would change according to the row in the first table. Commented Jan 5, 2012 at 19:51
  • (I think my formatting preserved the initial idea). Do you want to do something like a left outer join to tableB? Commented Jan 5, 2012 at 19:55

3 Answers 3

2
from a in tableA
from b in tableB
where a >= b.minX && a <= b.maxX
select a;
Sign up to request clarification or add additional context in comments.

4 Comments

Is this even legal? And what is the lambda expression equivalent?
@Fernando: Of course it's legal, it's a Cartesian join and a filter on the result. And what do you mean by "lambda expression equivalent?"
example: myThings.Where(thing => thing <= 25)
@Fernando: A double from clause translates to a SelectMany, if that's what you're asking.
0

Something like this?

var fdts = from fdt in context.FeuilleDeTemps.Include("FeuilleDeTempsJournees")
           where
               (employes.Contains(fdt.ID_Employe)) &&
               (fdt.DateDepart <= date) &&
               (fdt.DateFin >= date)
           orderby fdt.ID_Employe
           select fdt;

or in your case

var v = from a in context.tableA.Include("tableB")
        where
            a.x < b.minX &&
            a.x > b.maxX
        select v;

Comments

0

You asked specifically for the dotted syntax?

        var tableA = new[] {1, 2, 3, 4};
        var tableB = new[]
                         {
                             new {minX = 1, maxX = 2},
                             new {minX = 3, maxX = 6},
                             new {minX = 5, maxX = 7},
                             new {minX = 6, maxX = 8},
                         };
        var res = tableA.SelectMany(a => tableB, (a, b) => new {a, b}).Where(t => t.a >= t.b.minX && t.a <= t.b.maxX)
                .Select(t => t.a);

2 Comments

Thanks for the additional help!
You're welcome. You can, of course, optimize by putting in the check predicate within the first argument passed to SelectMany, ie "a=>tableB.Where(b=>((a<b.maxX)&&(a>b.minX))) " so you don't have to new() the tuple before checking.

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.