您好,登錄后才能下訂單哦!
本篇文章為大家展示了Linq中怎么自定義組合查詢(xún),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
這個(gè)表單將Linq自定義組合條件提交后臺(tái),我先將它封裝成條件對(duì)象的數(shù)組。
/// /// 條件 /// public class Condition { /// /// 字段 /// public string Field { get; set; } /// /// 表達(dá)式 /// public string Operator { get; set; } /// /// 值 /// public string Value { get; set; } /// /// 關(guān)系 /// public string Relation { get; set; } /// /// /// /// /// /// /// /// public static Condition[] BuildConditions(string[] fileds,string[] operators,string[] values,string[] relations) { if (fileds == null || operators == null || values == null || relations == null) { return null; } Condition[] conditions = new Condition[fileds.Length]; try { for (int i = 0; i < conditions.Length; i++) { conditions[i] = new Condition() { Field = fileds[i], Operator = operators[i], Value = values[i], Relation = relations[i] }; } } catch { return null; } return conditions; } }
實(shí)際上,編譯器是把Linq自定義表達(dá)式編譯成expression tree的形式,我只需要將條件對(duì)象數(shù)組轉(zhuǎn)換為expression tree就可以了。
我先將一個(gè)條件轉(zhuǎn)化為一個(gè)簡(jiǎn)單的expression。
/// /// /// /// /// /// private static Expression ConditonToExpression(Condition condition,Expression parameter) { Expression expr = null; Type type = typeof(EDM_Resource); PropertyInfo pi = type.GetProperty(condition.Field); Expression left = Expression.Property(parameter, pi); object value = Convert.ChangeType(condition.Value, pi.PropertyType); Expression right = Expression.Constant(value); switch (condition.Operator) { case "=": expr = Expression.Equal(left, right); break; case "<": expr = Expression.LessThan(left, right); break; case "<=": expr = Expression.LessThanOrEqual(left, right); break; case ">": expr = Expression.GreaterThan(left, right); break; case ">=": expr = Expression.GreaterThanOrEqual(left, right); break; } return expr; }
然后組合,變成一個(gè)lamda表達(dá)式,追加到where上。
/// /// /// /// /// /// /// /// /// public IList FindByGroup(EDM_ResGroup resGroup, Condition[] conditions, int first, int limit, out int count) { using (ShengjingEDM2Entities context = new ShengjingEDM2Entities()) { IQueryable result = DoFindByGroup(resGroup, context); ParameterExpression parameter = Expression.Parameter(typeof(EDM_Resource), "r"); Expression body = null; if (conditions != null && conditions.Length > 0) { body = ConditonToExpression(conditions[0], parameter); for (int i = 1; i < conditions.Length; i++) { Expression right = ConditonToExpression(conditions[i],parameter); body = conditions[i - 1].Relation.ToUpper().Equals("AND") ? Expression.And(body, right) : Expression.Or(body, right); } } if (body != null) { Expression<FUNC<EDM_RESOURCE, < SPAN>bool>> expr = Expression.Lambda<FUNC<EDM_RESOURCE, < SPAN>bool>>(body, parameter); result = result.Where(expr); } result = result.OrderByDescending<EDM_RESOURCE, < SPAN>int>(r => r.ResourceID); count = result.Count(); return result .Skip(first) .Take(limit) .ToList(); } }
原來(lái)Linq自定義這么強(qiáng)大,這么爽,比拼where條件的方法優(yōu)雅多了,開(kāi)發(fā)效率也是提高不少,而且我發(fā)現(xiàn)性能也不錯(cuò),100萬(wàn)級(jí)的數(shù)據(jù)通過(guò)索引和分頁(yè)查詢(xún)還算可以。
上述內(nèi)容就是Linq中怎么自定義組合查詢(xún),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。