In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use LINQ+Ajax dynamic query". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use LINQ+Ajax dynamic query.
Train of thought:
1. The front desk issued a request to specify the modleName of the call and the filtering of some attributes.
For example, Author like,1 ModelName Article search author contains 1 object name article
two。 The background accepts the parameters passed by processing
3. Call the object based on the object name and filter
4. Return the corresponding page according to the object name
1. Foreground JS code
Foreground code
/ / backend list of any type / / = = function AjaxForList (duixiang, pageid) {var searchWords = $("# SearchWords") .val (); var searchType = $("# SearchType") .val (); var channelId = $("# list") .val (); var IsRecycle = $("# IsRecycle") .attr ("checked"); / / sort name var sortName = "CreaterData" / / = IsRecycle var Del = new Array (); Del.push ("="); / / alert (IsRecycle); if (IsRecycle) {Del.push (1);} else {Del.push (0);} / / = FullTitle / / 2010-3-22 0:00:00 var FullTitle = new Array (); FullTitle.push ("like"); FullTitle.push ("1") / / = FullTitle var Author = new Array (); Author.push ("like"); Author.push ("1"); / / = IsAudit var IsAudit = new Array (); IsAudit.push ("="); IsAudit.push ("0") / = = $.ajax (/ / {type: "POST", url: "/ Admin/UserKJ/AjaxForList", data: "ModelName=" + duixiang + "& & PageId=" + pageid + "& & sortName=" + sortName + "& & Del=" + Del + "& & FullTitle=" + FullTitle + "& Author=" + Author + "& IsAudit=" + IsAudit, dataType: "html" BeforeSend: function (XMLHttpRequest) {/ / $(".PagerModeList") .html ("= LODING!! =") }, success: function (html) {/ / if (m! = null) / / alert (html); $(".PagerModeList") .html (html);}, complete: function (XMLHttpRequest, textStatus) {/ / HideLoading () }, error: function () {/ / request error handling $(".PagerModeList") .html ("load failed");}) / / end $.ajax}; / / end AjaxRequest () {
2 backend receiving:
Merge two custom dictionaries into one key-action-value dictionary
Code
Public class SearchPage {public string ModelName {set; get;} public int PageId {set; get;} public string sortName {get; set;} / RequestFormDictionary / public ThreeDictionary RFD = new ThreeDictionary (); public SearchPage () {RFD = new ThreeDictionary () } public void InitDictionary (System.Web.HttpRequestBase httpRequestBase) {var requestForm = httpRequestBase.Form; foreach (var collection in requestForm.AllKeys) / / x = Article,0,1,0,false ointxx journal xx; {string actionAndvalue = requestForm.Get (collection); if (string.IsNullOrEmpty (collection)) continue / / separate the parameters List templist = actionAndvalue.splitString (','); if (templist.Count ())
< 2) continue; RFD.Add(collection, templist[0], templist[1]); } } public string Dictionary2SQL() { StringBuilder stringBuilder = new StringBuilder(); int count = 0; foreach (var keyActon in RFD.keyActons) { if (keyActon.Value == "Value") continue; // @1特别指定 如果 是 动作是 Value 则 表示 直接意思 string value = RFD.keyValues[keyActon.Key]; if (count != 0) stringBuilder.Append(" and "); count++; if (keyActon.Value == "like") { // continue; //FullTitle.Contains(\"11\") stringBuilder.Append(string.Format(" {0}.Contains(\"{2}\") ", keyActon.Key, keyActon.Value, value)); continue; } stringBuilder.Append(string.Format(" {0} {1} {2} ", keyActon.Key, keyActon.Value, value)); } return stringBuilder.ToString(); } } 3.根据 对象名 调用 对象 并过滤 代码 private readonly Repository2.IRepositoryResolver IRR; public UserKJController() { IRR = CMSContainer.GetContainer()[typeof(IRepositoryResolver)] as IRepositoryResolver; } 这里用到了 一个 CASTLE 和 一个 Repository 相信 能看到这里的 应该都知道这些吧。 不知道的话 文末 会给出 链接。 AjaxForList public ActionResult AjaxForList(SearchPage searchPage){// Response.Write(DateTime.Now);searchPage.InitDictionary(Request);//把 字符串装进字典里面IRepository ir = IRR.GetRepository(searchPage.ModelName.str2type()); //根据类型得到要操作的类var iq = ir.GetAll();// 得到所有iq = iq.Order(searchPage.sortName); // 并 排序iq = iq.Where(searchPage.Dictionary2SQL());//根据 字典 自动 过滤PagerInfo pif = new PagerInfo(iq.Count(), searchPage.PageId); //页信息iq = iq.Skip(pif.PageSize * (pif.CurrentPage - 1)).Take(pif.PageSize); //分页PagerIndexpagerIndex2 = new PagerIndex(pif, iq);//装配//Response.Write(DateTime.Now.ToLongTimeString());return View("AjaxListFor" + searchPage.ModelName, pagerIndex2); //返回} 这里 用到了 System.Linq.Dynamic; 来做核心 的 排序和过滤 。 然后放出一个 firebug的 图 通过我自己代码输出耗费的时间 不用1s 而且还是我本机的破机子,1G的内存条郁闷要死。 比我原来预想的 泛型会很耗性能。 感觉好了很多。 这是我没找到 Dynamic 之前 自己做的一个 轮子 辛辛苦苦做出来 还不支持 nullable的 类型 , 想了办法 二次调用 构造 了 c.WithPic.Value的表达式 {Table(KF_Articles).OrderBy(ArticleID =>ArticleID.ArticleID) .Where (item = > (item.Del = 0)) .Where (item = > (item.WithPic.Value = 1))}
Result
I discussed it with brother Chung Dian, but there was no result.
Think about it again when you have time. I hope the audience can give us some advice.
EqualValue, my wheel.
/ / search/// A property is a value / attribute / / value / Action:
< = 等 /// public IQueryable EqualValue(string pifName, object value, ActionType action, IQueryable iq){if (iq == null) throw new Exception(" IQueryable iq 为空");if (typeof(T).GetProperty(pifName) == null)throw new Exception(pifName + " in " + typeof(T).Name);//1 构造 参数 item 即 Tvar itemParameter = Expression.Parameter(typeof(T), "item");IQueryable iqTemp = iq as IQueryable;// 转换成 model 的 那种类型// 判断是否是 Nullable 的 如果是 的话 就 把类型转换成对应的 非 nullable 的Type tempType = typeof(T).GetProperty(pifName).PropertyType;if (tempType.FullName.Contains("Nullable")){//组建一个表达式树来创建一个参数}var tempType2 = tempType.Nullable2Not();value = Convert.ChangeType(value, tempType2);//取出他的值var left = Expression.Property(// 左边的参数itemParameter,pifName // 属性的 名字);var right = Expression.Constant(value);//Constant 常量 右边的值#region //=========================各种 action 操作BinaryExpression ep;if (action == ActionType.Equal){if (tempType.FullName.Contains("Nullable")){ParameterExpression param =Expression.Parameter(typeof(T), "item");// item2//item2.pifName =>Item2.WithPic// item2.WithPic.valueMemberExpression param11 = Expression.Property (param, pifName); MemberExpression param12 = Expression.Property (param11, "Value"); / / typeof (T) .GetProperty (pifName). GetType (). GetProperty ("Value"); / / ParameterExpression param2 = / / Expression.Parameter (typeof (T) .GetProperty (pifName). GetType (), "Value"); / / build expression tree: c.ContactNameep = Expression.Equal (param12, right);} else {ep = Expression.Equal (left, right) }} else if (action = = ActionType.GreaterThan) {ep = Expression.GreaterThan (left, right);} else if (action = = ActionType.GreaterThanOrEqual) {ep = Expression.GreaterThanOrEqual (left, right);} else if (action = = ActionType.LessThan) {ep = Expression.LessThan (left, right);} else if (action = ActionType.LessThanOrEqual) {ep = Expression.LessThanOrEqual (left, right);} else if (action = = ActionType.NotEqual) {ep = Expression.NotEqual (left, right);} else {else (else + "throw new Exception!") } # endregionvar whereExpression = Expression.Lambda// construction expression (ep, new [] {itemParameter}); return iqTemp.Where (whereExpression)
Pointing out a small mistake of brother Xiao Kun, Dynamic also supports search.
/ / continue; FullTitle.Contains (\ "11\") stringBuilder.Append (string.Format ("{0} .Concluded (\" {2}\ "), keyActon.Key, keyActon.Value, value)); now that you have a better understanding of" how to use LINQ+Ajax dynamic query ", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.