System.Linq.Dynamic.Core icon indicating copy to clipboard operation
System.Linq.Dynamic.Core copied to clipboard

Documentation: improve enum string literal example

Open zspitz opened this issue 5 years ago • 1 comments

The following example:

using (var context = new EntityContext())
{
    var list = context.Customers.Where("OrderDate.DayOfWeek = @0", DayOfWeek.Monday); 
}

is supposed to demonstrate how Dynamic LINQ parses a string literal embedded inside a Dynamic LINQ string as an enum. But the example only shows how it's possible to pass in an actual enum value to Dynamic LINQ, via the @0 parameter. A better example would be:

using (var context = new EntityContext())
{
    var list = context.Customers.Where("MyEnumProperty = \"FirstValue\""); 
}

where the enum value is itself embedded as a string literal inside the Dynamic LINQ string.

Also, it should be noted that as of #450 , all the enum types defined in mscorlib are available directly, not as string literals. So to use DayOfWeek, it doesn't need to be wrapped in ":

using (var context = new EntityContext())
{
    var list = context.Customers.Where("OrderDate.DayOfWeek = DayOfWeek.Monday"); 
}

zspitz avatar Oct 21 '20 16:10 zspitz

To sum up, given:

class Foo {
    public MyEnum MyEnum1 { get; set; };
    public DayOfWeek DOW { get; set; };
}
enum MyEnum {
    First
}

a constant enum value can be specified using it's name in one of three ways:

  • As an embedded string literal of the name without the type name:
    MyEnum1 = "First" "First" = MyEnum1
  • Passing in the enum value as a Dynamic LINQ argument: MyEnum1 = @0 @0 = MyEnum1 while passing in MyEnum.MyEnum1
  • If the enum is one of the predefined enums (as of #450, all the enums in mscorlib), or explicitly added to the accessible types using a custom type provider, the enum value can be referenced directly via the type name: DOW = DayOfWeek.Tuesday DayOfWeek.Tuesday = DOW.

zspitz avatar Nov 17 '20 07:11 zspitz