8

I'm using vb.net 2013. I try to use a tool that I have installed using NUGET. EntityFramewrok.Utilities / link : https://github.com/MikaelEliasson/EntityFramework.Utilities This is an open source dll and the whole code can be downloaded. From this DLL , I'm trying to use the "IncludeEFU" method. In the link above , is a code that I use on C# project and works :

var result = db.Contacts
.IncludeEFU(db, x => x.PhoneNumbers
.Where(n => n.Number == "10134")
.OrderBy(p => p.ContactId)
.ThenByDescending(p => p.Number))
.ToList();

I try to use on my VB.net application the same code like this :

Dim result = db.Contacts _
.IncludeEFU(db, Function(x) x.PhoneNumbers _
.Where(Function(n) n.Number = "10134")_
.OrderBy(Function(p) p.ContactId) _
.ThenByDescending(Function(p) p.Number)).ToList()

But I'm getting an error :

An unhandled exception of type 'System.ArgumentException' occurred in     EntityFramework.Utilities.dll

Additional information: Could not find a MemberExpression

Inspecting one by one the files in the dll's project ( that can be downloaded on the link ) , I see that the error message that I'm getting come from this sub :

private static PropertyInfo SetCollectionModifiersAndGetChildProperty<T, TChild>(Expression<Func<T, IEnumerable<TChild>>> collectionSelector, List<MethodCallExpression> childCollectionModifiers)
        where T : class
        where TChild : class
    {
        var temp = collectionSelector.Body;
        while (temp is MethodCallExpression)
        {
            var mce = temp as MethodCallExpression;
            childCollectionModifiers.Add(mce);
            temp = mce.Arguments[0];
        }
        childCollectionModifiers.Reverse(); //We parse from right to left so reverse it
        if (!(temp is MemberExpression))
        {
            throw new ArgumentException("Could not find a MemberExpression", "collectionSelector");
        }

        var childProp = (temp as MemberExpression).Member as PropertyInfo;
        return childProp;
    }

Look at the line :

throw new ArgumentException("Could not find a MemberExpression", "collectionSelector");

Why this is working on a C# project , and produce this error on a VB.net project ? How can I resolve this problem ? Thank you !

Edited : I try to make some changes in that sub :

    ...

    while (temp is MethodCallExpression)
            {
                var mce = temp as MethodCallExpression;
                childCollectionModifiers.Add(mce);
                temp = mce.Arguments[0];
            }
        while (temp is UnaryExpression)
        {
            var ue = temp as UnaryExpression;
            temp = ue.Operand;
        }
        .....

After I rebuild the dll file , and now the error message is disappear. But can anyone confirm that this is a correct solution ?

alex
  • 694
  • 3
  • 15
  • 35
  • 3
    Did you try it without the line continuations? – Rowland Shaw Feb 02 '15 at 21:30
  • 1
    What type is `x.PhoneNumbers`? – SLaks Feb 02 '15 at 21:31
  • 4
    Seems like you have a misplaced close parentheses. You should be closing the parentheses on `Include` but it is not closed until the end. Not sure why it works in C# but not in VB, however. – D Stanley Feb 02 '15 at 21:32
  • 1
    It appears to be a difference of how lambda expressions are done in C# vs VB. Not sure what the resolution is, however. Maybe the answer in http://stackoverflow.com/questions/16939535/linq-expression-getvalue-in-vb helps? – NextInLine Feb 02 '15 at 21:35
  • it would be nice to see the `Class Header` as well as verify if you have the dll in the reference node.. also check if `CopyTo Local = true` – MethodMan Feb 02 '15 at 21:36
  • @DStanley: That won't work in C# either. That C# code in the question is copy/paste from the project readme, but the readme has the parens in the right place. https://github.com/MikaelEliasson/EntityFramework.Utilities/blob/master/README.md – Moby Disk Feb 02 '15 at 21:38
  • @MethodMan: That cannot possibly cause this kind of error. – SLaks Feb 02 '15 at 21:39
  • 3
    This is probably a bug in the library; VB lambdas are probably compiled slightly differently. – SLaks Feb 02 '15 at 21:40
  • I can confirm that this is working on a C# project.The problems is in the sub that I have posted. If the problems are vb lambdas , does anyone knows a way how to resolve it ? – alex Feb 02 '15 at 22:03
  • 1
    It seems that someone reported this problem to the above link , but still no solution : https://github.com/MikaelEliasson/EntityFramework.Utilities/issues/29 – alex Feb 02 '15 at 22:06
  • 3
    I'm voting to close this question as off-topic because confirming that the fix which is proposed in the EntityFramework.Utilities project is outside the scope of Stack Exchange. A pull request to add that check on the github project, and unit tests for consumption from VB, would seem a better course. – Tetsujin no Oni Feb 10 '15 at 21:36

2 Answers2

1

The original EntityFramework.Utilities DLL may have been built with a different .NET "Target framework" than that of your VB.NET application.

This may explain why the original DLL did not work while your rebuilt DLL (using the same .NET "Target framework" defined for your project in VS.NET 2013) did work.

To examine the "Target framework" for VS.NET, right click on the applicable project in the Solution Explorer, then click on the "Properties" option. The "Application" tab will display a "Target framework:" label with a pulldown input of the .NET Framework version numbers available. The current setting of this pulldown is the .NET "Target framework" used to make your build in VS.NET 2013.

JohnH
  • 1,920
  • 4
  • 25
  • 32
0

This error is because VB and C# expression trees have a different structure. I wat assuming they would be the same so VB is not working right now. Hopefully I get around to building a new version soon where this is fixed.

Mikael Eliasson
  • 5,157
  • 23
  • 27