none
Refactor Linq to SQL queries RRS feed

  • Question

  • I have a few Linq quries. Semantically, they are

    1. a join b join c join d where filter1(a) && filter2(c) && filter3(d)

    2. a join b join c where filter1(a) && filter2(c)

    3. a join b join c join e where filter1(a) && filter2(c) && filter4(e)

    ...

    I want to be able to factor out the shared part:

    a join b join c where filter1(a) && filter2(c) 

    and dynamically append join d and filter3(d)

    Is there a way to do this? I am already using the Predicate Buidler to dynamically build conditionals (filters).

    EDIT: I am using Linq-to-SQL. EDIT: The base query looks like:

    from a in As.AsExpandable() 
    join b
    in Bs on a.Id equals b.PId 
    join c
    in Cs on b.Id equals c.PId 
    where filter1
    (a) && filter2(b) && filter3(c) 
    select new A { ... } 

    filters are predicates in Predicate Builder. The type of the query is IQueryable. Next, I'd like to join this with d

    from a in BaseQuery() 
    join d
    in D on a.Id equals d.PId 

    Currently join d .. causes a compilation error: The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to Join

    Wednesday, February 22, 2012 8:21 PM

Answers

  • As for me, when you are assigning some complex query to the variable, in the end it always has some type (anonymous or predefined, doesn't matter).

    once you've applied your result to the variable you can't change it until you declare new one.

    I had a problem like this and the best solution was pushing all login in SQL. The result was that my solution was simple and very clear


    Please mark as read if helped.
    Also visit my blog http://msguy.net/

    Friday, March 9, 2012 1:42 PM

All replies

  • Hi xinonnet,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 6, 2012 9:15 AM
    Moderator
  • As for me, when you are assigning some complex query to the variable, in the end it always has some type (anonymous or predefined, doesn't matter).

    once you've applied your result to the variable you can't change it until you declare new one.

    I had a problem like this and the best solution was pushing all login in SQL. The result was that my solution was simple and very clear


    Please mark as read if helped.
    Also visit my blog http://msguy.net/

    Friday, March 9, 2012 1:42 PM
  • I would write it pretending i made a dictionary, and without any joins at all

    var dictionary = from row in C

                 where c.B.A.Name = "Bob"

                    && c.B.Name="Carl"

                 select c.B.A.Id

    //This would build up a dictionary that is IQueryable, and thus deferred execution, then you can just use that.

    var query = from row in D where

                    dictionary.Contains(row.DId)

                   select D.A;

    But mabye that is just because I like the dot syntax.

    Tuesday, March 13, 2012 3:53 PM