How to use Enums with Dynamic Linq?

I would like to use enums in my dynamic LINQ queries.

Is it possible, and if, how?

Consider the code below:

using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Room aRoom = new Room() { Name = "a Room" }; Room bRoom = new Room() { Name = "b Room" }; Room cRoom = new Room() { Name = "c Room" }; House myHouse = new House { Rooms = new List<Room>(new Room[] { aRoom }), MainRoom = aRoom }; House yourHouse = new House() { Rooms = new List<Room>(new Room[] { bRoom, cRoom }), MainRoom = bRoom }; House donaldsHouse = new House() { Rooms = new List<Room>(new Room[] { aRoom, bRoom, cRoom }), MainRoom = aRoom }; var houses = new List<House>(new House[] { myHouse, yourHouse, donaldsHouse }); // MainRoom.Name = \"a Room\" and Rooms.Count = 3 or // ????????????????????????? var aRoomsHouses = houses.AsQueryable<House>().Where("MainRoom.Type = \"RoomType.Kitchen\""); Console.WriteLine("aRoomsHouses count = {0}", aRoomsHouses.Count()); Console.ReadKey(); } } public class House { public string Address { get; set; } public double Area { get; set; } public Room MainRoom { get; set; } public List<Room> Rooms { get; set; } } public class Room { public double Area { get; set; } public string Name { get; set; } public RoomType Type { get; set; } } public enum RoomType { Kitchen, Bedroom, Library, Office } } 
+6
source share
5 answers

It works:

 houses.AsQueryable<House>() .Where("MainRoom.Type = ConsoleApplication2.RoomType.Kitchen") 
+4
source

I ran into the same problem and tried the marked answer given by @Steve Wilkes, but this did not work for me !! Then I discovered that dynamic LINQ has HTML documentation in the same package, which mentioned that Enums can be specified as String literals.

 houses.AsQueryable<House>().Where("MainRoom.Type = \"Kitchen\"") 

What worked for me.

+16
source

This should work

 houses.AsQueryable<House>().Where(rs=>rs.MainRoom.Type == RoomType.Kitchen); 

Why do you need dynamic linq in this case? What result do you expect

To my preference, the use of an error string should be avoided. If your class or property name has changed, you cannot find the error until you encounter it.

Use expression rather

  Expression<Func<House, bool>> filter = (p) => p.MainRoom.Type == RoomType.Kitchen; filter = (p) => p.MainRoom.Area > 200; filter = (p) => p.Rooms.Sum(rs => rs.Area) > 500; filter = (p) => p.Address.Contains("abc"); filter = (p) => p.Area > 200; ... var aRoomsHouses = houses.AsQueryable<House>().Where(filter); 

You can create an expression in which you decide which string filter to use. It's better to create a static class, or maybe a switch statement that gives you a different type of expression that you can use as the where argument.

+1
source

additionally another option for using the parameter

 var aRoomsHouses = houses.AsQueryable<House>().Where("MainRoom.Type = @0",RoomType.Kitchen); 
+1
source

To add a new Enum type to dynamic linq, you must add the following code:

 typeof(Enum), typeof(T) T : Enum type 

in predefined dynamic types This works for me;

-1
source

Source: https://habr.com/ru/post/895127/


All Articles