1
我想創建linq查詢,該查詢應該足夠動態以獲取特定類的所有屬性,併爲linq中的'where'和'select'構建表達式樹。如何將LINQ查詢轉換爲動態查詢?
有一個域模型由bool屬性和數字屬性組成。數字屬性將用於範圍。
例子:
域模型
public class MakeMeReal
{
public bool isA { get; set; }
public bool isB { get; set; }
public int Type1 { get; set; }
public double Type2 { get; set; }
public double Type3 { get; set; }
public string Type4 { get; set; }
}
輸入模型:
public class LinqDynamic
{
public bool isA { get; set; }
public bool isB { get; set; }
public int lowerRangeForTyp1 { get; set; }
public int UpperRangeForTyp1 { get; set; }
public double LowerRangeForType2 { get; set; }
public double UpperRangeForType2 { get; set; }
}
簡單的查詢:
public void query(LinqDynamic dynamicInput)
{
SampleDbContext db = new SampleDbContext();
var result = from m in db.MakeMeReal
where m.isB == dynamicInput.isB && m.isA == dynamicInput.isA &&
m.Type1 >= dynamicInput.lowerRangeForTyp1 && m.Type1 < dynamicInput.UpperRangeForTyp1 &&
m.Type2 >= dynamicInput.LowerRangeForType2 && m.Type1 < dynamicInput.UpperRangeForType2
select new { a = m.Type1, b = m.Type2, c = m.Type3, d = m.Type4 };
}
我想建立查詢將經歷域模型和我將構建表達式樹,它將針對運行時使用'LinqDynamic'類作爲輸入參數提供的輸入運行。
我有超過10個布爾值和30個範圍選擇器,布爾值和範圍選擇器的數量會根據項目需求不斷變化,所以每次更改查詢都會很頭疼。我想使其動態的,因此,它會是不可知的任何變化
我讀到https://msdn.microsoft.com/en-us/library/bb397951.aspx和我不能在這裏發表由於缺乏信譽點一些其他環節
但我我不知道如何實現它