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 ?

11
  • 3
    Did you try it without the line continuations? Commented Feb 2, 2015 at 21:30
  • 1
    What type is x.PhoneNumbers?
    – SLaks
    Commented Feb 2, 2015 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
    Commented Feb 2, 2015 at 21:32
  • 3
    This is probably a bug in the library; VB lambdas are probably compiled slightly differently.
    – SLaks
    Commented Feb 2, 2015 at 21:40
  • 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. Commented Feb 10, 2015 at 21:36

2 Answers 2

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.

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.