Unexpected return type when a Func is used in Join query projection

Viewed 36

I have a join query and I want to project one of the objects being joined with some minor changes in it. I figured out that the best way to do this would be to project a Func that makes the changes that I need.

Of the two lists that I am joining, one is a List<dynamic> and the other is List<ReceiptBE>.

My Func is supposed to return a ReceiptBE, but in the join projection when I call the Func, the result of the join query is of type IEnumerable<dynamic>. However, when I convert the same Func to an Inline method, the result of the same join query is of type IEnumerable<ReceiptBE>.

Func Implementation:

public void Method1()
{
        Func<ReceiptBE, dynamic, ReceiptBE> myFunc = (receipt, link) =>
        {
                receipt.something = link.something.ToString();
                return receipt;
        };

        var receiptDetails = new List<ReceiptBE>();
        var receiptLinks = new List<dynamic>(); 

        //result is IEnumerable<dynamic>
        //why is result not IEnumerable<ReceiptBE> ?
        var result = from receiptLink in receiptLinks
                                join receiptDetail in receiptDetails on receiptLink.something.ToString() equals receiptDetail.something
                                select myFunc(receiptDetail, receiptLink);

        //result2 is IEnumerable<ReceiptBE>
        var result2 = from receiptLink in receiptLinks
                                join receiptDetail in receiptDetails on receiptLink.something.ToString() equals receiptDetail.something
                                select myFunc(receiptDetail, receiptLink) as ReceiptBE;

}   

Local method implementation:

public void Method2()
{
        ReceiptBE myInlineMethod(ReceiptBE receipt, dynamic link)
        {
                receipt.something = link.something.ToString();
                return receipt;
        }

        var receiptDetails = new List<ReceiptBE>();
        var receiptLinks = new List<dynamic>(); 

        //result is IEnumerable<ReceiptBE>
        var result = from receiptLink in receiptLinks
                                join receiptDetail in receiptDetails on receiptLink.something.ToString() equals receiptDetail.something
                                select myInlineMethod(receiptDetail, receiptLink);
}

Also, when I write myInlineMethod as a private class method, again the type of result becomes IEnumerable<dynamic>

What I want in the result is IEnumerable<ReceiptBE> and because local method is a c# 7 feature (few people in organization use VS2015), I am stuck with the Func implementation with a cast.

Please guide me to the docs where I can read about this behavior of Func.

0 Answers
Related