如何使用枚举与动态的LINQ? [英] How to use Enums with Dynamic Linq?
本文介绍了如何使用枚举与动态的LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用枚举在我的动态LINQ查询。
是否有可能,如果,怎么办?
考虑code波纹管:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Linq.Dynamic;
命名空间ConsoleApplication2
{
类节目
{
静态无效的主要(字串[] args)
{
房间aRoom =新房间(){名称=客房};
房间扫帚=新房间(){名称=B室};
客房克鲁姆=新房间(){名称=C室};
众议院的MyHouse =新房
{
客房=新的名单,其中,客房及GT;(新房间[] {} aRoom)
MainRoom = aRoom
};
众议院yourHouse =新楼()
{
客房=新的名单,其中,客房及GT;(新房间[] {扫帚,克鲁姆}),
MainRoom =扫帚
};
众议院donaldsHouse =新楼()
{
客房=新的名单,其中,客房及GT;(新房间[] {aRoom,扫帚,克鲁姆}),
MainRoom = aRoom
};
VAR房子=新的名单,其中,房屋及GT;(新房[] {的MyHouse,yourHouse,donaldsHouse});
// MainRoom.Name = \一房\和Rooms.Count = 3
// ?????????????????????????
VAR aRoomsHouses = houses.AsQueryable<房屋及GT;(),其中(MainRoom.Type = \RoomType.Kitchen \);
Console.WriteLine(aRoomsHouses数= {0},aRoomsHouses.Count());
Console.ReadKey();
}
}
公共类之家
{
公共字符串的地址{获得;组; }
公共双区{获得;组; }
公共房间MainRoom {获得;组; }
公开名单<房间>客房{获得;组; }
}
公共类房
{
公共双区{获得;组; }
公共字符串名称{;组; }
公共RoomType类型{获取;组; }
}
公共枚举RoomType
{
厨房,
卧室,
图书馆,
办公室
}
}
解决方案
本作品:
houses.AsQueryable<家>()
。凡(MainRoom.Type = ConsoleApplication2.RoomType.Kitchen)
I would like to use enumerations in my dynamic LINQ queries.
Is it possible, and if, how?
Consider the code bellow:
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
}
}
解决方案
This works:
houses.AsQueryable<House>()
.Where("MainRoom.Type = ConsoleApplication2.RoomType.Kitchen")
这篇关于如何使用枚举与动态的LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文