none
LINQ anonymous variable as parameters? RRS feed

  • Question

  • Dear all,

    var q = from b in TblB select b;

    And I can easily pass q into another method, becaues q is IQueryable<TblB>

    But how about:

    var q = from b in TblB
            from c in TblC
            select new {b, c};

    I need to pass q into another method (multiple times) to do add some filtering. The filtering is very complicated, I can not afford to duplicate the codes repeatedly inside the code where the query is formated.

    Is there a way for me to do that?

    Thanks in advance.
    Sunday, November 15, 2009 8:33 AM

Answers

  • Hi Chui,


    I am afraid passing anonymous types across methods is not supported. Although you can find some workarounds like this, but those are not recommended. To be on the safe side, you should create a new type, say IntermediateResult and return IQueryable<IntermediateResult> from your method.

    public class IntermediateResult
    {
      public TblA tblAEntity { get; set; }
      public TblB tblBEntity { get; set; }
    }
    


    Hope that helps.

    Regards,

    Syed Mehroz Alam
    My Blog | My Articles
    Sunday, November 15, 2009 12:19 PM
  • Replace

    var q = from b in TblB
            from c in TblC
            select new {b, c};

    with

    var q = from b in TblB
            from c in TblC
            select new IntermediateResult { YOUR_TBL_B_PROPERTY_NAME_HERE = b, YOUR_TBL_C_PROPERTY_NAME_HERE = c};

    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked as answer by Chui Kean Monday, November 16, 2009 11:37 PM
    Monday, November 16, 2009 12:44 AM
    Answerer

All replies

  • Hi Chui,


    I am afraid passing anonymous types across methods is not supported. Although you can find some workarounds like this, but those are not recommended. To be on the safe side, you should create a new type, say IntermediateResult and return IQueryable<IntermediateResult> from your method.

    public class IntermediateResult
    {
      public TblA tblAEntity { get; set; }
      public TblB tblBEntity { get; set; }
    }
    


    Hope that helps.

    Regards,

    Syed Mehroz Alam
    My Blog | My Articles
    Sunday, November 15, 2009 12:19 PM
  • Syed,

    I got the error below. Can you elaborate how it is done?


    Error 1 Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<Test.IntermediateResult>'. An explicit conversion exists (are you missing a cast?)

    Monday, November 16, 2009 12:26 AM
  • Replace

    var q = from b in TblB
            from c in TblC
            select new {b, c};

    with

    var q = from b in TblB
            from c in TblC
            select new IntermediateResult { YOUR_TBL_B_PROPERTY_NAME_HERE = b, YOUR_TBL_C_PROPERTY_NAME_HERE = c};

    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked as answer by Chui Kean Monday, November 16, 2009 11:37 PM
    Monday, November 16, 2009 12:44 AM
    Answerer
  • Great! It works... :)
    Monday, November 16, 2009 11:38 PM