Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: improve enum string literal example #434

Open
zspitz opened this issue Oct 21, 2020 · 1 comment
Open

Documentation: improve enum string literal example #434

zspitz opened this issue Oct 21, 2020 · 1 comment
Assignees

Comments

@zspitz
Copy link

zspitz commented Oct 21, 2020

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
Copy link
Author

zspitz commented Nov 17, 2020

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 Support the enum UriKind #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.

@StefH StefH self-assigned this Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants