public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public string Door { get; set; }
}
public class Member
{
public int Id { get; set; }
public string Name { get; set; }
public string Level { get; set; }
public Address MyAddress { get; set; }
}
public class DynamicLinqQueryTest
{
public List<Member> _list = new List<Member>();
public void Initilize()
{
for (int i = 0; i < 20; i++)
{
Member member = new Member();
member.Id = i + 1;
member.Name = "Member" + (i + 1);
member.MyAddress = new Address()
{
Id = i + 1,
Street = "Street" + (i + 1),
Door = "Door" + (i + 1)
};
member.Level = "Level" + (i + 1);
_list.Add(member);
}
}
public void Test()
{
Initilize();
//var members = (from m in _list select m).AsQueryable<Member>().Take<Member>(5).Where("d=@0 and Level=@1", 2, "Level2").
// Where("Name.Contains(@0)", "6").OrderBy("MyAddress.Street, Level desc").Select("new(Id as MyId, Level)");
var members2 = (from m in _list select m).AsQueryable<Member>().Where(" Id > 3")
.OrderBy("Id");
foreach (dynamic member in members2)
{
int Id = member.Id;
string name = member.Name;
Console.WriteLine(name +"----"+ Id);
}
var members = (from m in _list select m).AsQueryable<Member>().Take<Member>(5).Where("Id=@0 and Level=@1", 2, "Level2").
Where("Name.Contains(@0)", "2").OrderBy("MyAddress.Street, Level desc").Select("new(Id as MyId, Level)");
foreach (dynamic member in members)
{
int Id = member.MyId;
string name = member.Name;
Console.WriteLine(name + "----" + Id);
}
}
}