0

I'm using VB in a project, together with EF.PLUS https://entityframework-plus.net/

I want to use FirstOrDefaultDynamic, but I didn't found any example for usage in VB - all examples are in C#.

Specifically I have an array of array of strings, and I want to find an element in it:

dim a = {
    {"a", "a1"},
    {"b", "b1"},
    {"c", "c1"}
}

dim elem = a.FirstOrDefaultDynamic("x(0) = y", new with {y="a"})

But I get error on evaluating the expression.

FirstOrDefaultDynamic accepts only string expression as parameter, not predicate (function).

The C# example is

var list = ctx.WhereDynamic(x => "x > 2").ToList();

but it still accepts a function returning an string expression which is to be evaluated.
Any idea on the VB syntax?

GSerg
  • 76,472
  • 17
  • 159
  • 346
bzamfir
  • 4,698
  • 10
  • 54
  • 89
  • The C# example uses `WhereDynamic`. Do you have a C# example that uses your `FirstOrDefaultDynamic`? – GSerg Oct 09 '21 at 15:47
  • No, but the WhereDynamic call in VB also accepts only string argument, so what should work for WhereDynamic should work for FirstOrDefaultDynamic as well – bzamfir Oct 09 '21 at 15:49
  • 2
    I don't see how, given the text of the error message that you have quoted. `x => "x > 2"` is `Function(x) "x > 2"` in VB, which is the opposite of "accepts only string expression as parameter, not predicate". – GSerg Oct 09 '21 at 15:50
  • Could be that you misunderstood the message, as `x => "x > 2"` can be [either](https://stackoverflow.com/q/793571/11683) `Func` or `Expression>`. – GSerg Oct 09 '21 at 15:58
  • Possible. Still it requires the parameter as string. Any idea how to write the condition I need as Expression> in VB? – bzamfir Oct 09 '21 at 16:10
  • 2
    It's `Function(x) "x > 2"` like I said. – GSerg Oct 09 '21 at 16:21
  • I tried, but it cannot compile. The error is something like "Delegate cannot convert to string" – bzamfir Oct 09 '21 at 18:41
  • 1
    Then it does not expect a function in the first place, which is not what the C# example does, like I originally noted. – GSerg Oct 09 '21 at 18:59
  • Maybe you should start with a type that implements `IEnumerable`. – Gert Arnold Oct 09 '21 at 20:05
  • I did it with a strongly typed array and normal FirstOrDefault, but I was curious how this can be implemented the way I described here with a dynamic object – bzamfir Oct 11 '21 at 13:27
  • Well, not by LINQ, just classic for loops. Multi-dimensional arrays don't implement `IEnumerable`, the prerequisite for LINQ. – Gert Arnold Oct 12 '21 at 10:13
  • It might help to look at how the `...OrDefaultDynamic` extension methods are declared. What is the type of the argument? The Object Browser window might help. – Craig Oct 12 '21 at 13:58

2 Answers2

1

Disclaimer: I'm the owner of the project Entity Framework Plus

Only the C# syntax is supported.

So if you want to use any Dynamic method such as FirstOrDefaultDynamic from this library, you will need to stick with the C# syntax.

There is no plan to support VB syntax.

bzamfir
  • 4,698
  • 10
  • 54
  • 89
Jonathan Magnan
  • 10,874
  • 2
  • 38
  • 60
0

Just to clarify the answer from Jonathan Magnan :

The expression string used as parameter should use C# syntax, even if the call is made from VB.

Usage example:

Dim elem = a.FirstOrDefaultDynamic("x => x[0]=y", New With {.y = "MyTestVal"})
bzamfir
  • 4,698
  • 10
  • 54
  • 89