最近做的array string类型对比。这个可能比较复杂,用的是linq 是请教别人的,我在这里记录一下
jquery 方法里面的数组
function arrtxt() {
var arrt= [];
arrt.push('测试', '女', '124563', '');
arrt.push('小城市', '女', '8593431', '66867857857');
arrt.push('小路与', '男', '8593431', '498464');
var arrs = [];
arrs.push('小于', '女', '123123123', '' );
arrs.push('小城市', '女', '8593431', '66867857857');
arrs.push('小周', '男', '8593431', '498464');
$.ajax({
url: "ArrList.ashx?opt=arr&arrt=" + arrt+ "&arrs =" + arrs ,
type: "post",
success: function (date) {
var u = $.parseJSON(date);
var kref = u.ker
var tdker = u.td
var tab="<table>";
for (var i = 0; i< kref.length; i++) {
tab="<tr><td><span>" + kref[i].krname + "</span></td。";
tab+= "<td> <span>" + kref[i].krvisa + "</span></td></tr>";
}
tab+="</table>";
}
后台ashx页面
获取页面参数
1 public void ProcessRequest(HttpContext context) 2 { 3 context.Response.ContentType = "text/plain"; 4 context.Response.Charset = "utf-8"; 5 6 string opt = context.Request.Params["opt"].ToString().Trim(); 7 string html = ""; 8 switch (opt) 9 { 10 case "arr": 11 html = Getarr(context); 12 break; 13 } 14 context.Response.Write(htm); 15 }
1 #region Astring数组对比 2 public string GetArr(HttpContext context) 3 { 4 JObject jo = new JObject(); 5 JObject jotd = new JObject(); 6 JObject jakr = new JObject(); 7 string[] arrkr = new string[] {}; 8 #region 9 string kerens = ""; 10 if (!string.IsNullOrEmpty(context.Request.Params["arrt"])) 11 { 12 kerens = context.Request.Params["krres"].ToString().Trim(); 13 if (kerens.Length > 0 && kerens != "") 14 { 15 arrkr = kerens.Split(','); 16 } 17 } 18 string tdcz = ""; 19 string[] arrtd = new string[] { }; 20 if (!string.IsNullOrEmpty(context.Request.Params["arrs"])) 21 { 22 tdcz = context.Request.Params["tdds"].ToString().Trim(); 23 if (tdcz.Length > 0 && tdcz != "") 24 { 25 arrtd = tdcz.Split(','); 26 } 27 } 28 List<string> arrkrsd = new List<string>(); 29 string[] krstr = new string[] { }; 30 for (int i = 0, j = 0; i < Math.Ceiling(arrkr.Length / 4m); i++, j = j + 4) 31 { 32 krstr = (SplitArray(arrkr, j, 4)); 33 arrkrsd.Add(krstr[0] + "," + krstr[1] + "," + krstr[2] + "," + krstr[3] ); 34 arrkrsd.ToArray(); 35 } 36 var krsd = arrkrsd.Distinct().ToArray(); 37 List<string> arrtdsd = new List<string>(); 38 string[] tdstr = new string[] { }; 39 for (int i = 0, j = 0; i < Math.Ceiling(arrtd.Length / 4m); i++, j = j + 4) 40 { 41 tdstr = (SplitArray(arrtd, j, 4)); 42 arrtdsd.Add(tdstr[0] + "," + tdstr[1] + "," + tdstr[2] + "," + tdstr[3] ); 43 arrtdsd.ToArray(); 44 } 45 var tdsd=arrtdsd.Distinct().ToArray(); 46 var combine = 47 krsd.Concat(tdsd) // 合并 48 .Distinct() // 去掉重复项 49 .ToArray(); 50 51 // 通过Linq进行left join, 得到数组A 52 var Kre = 53 (from cItem in combine 54 join aItem in krsd on cItem equals aItem into ps 55 from p in ps.DefaultIfEmpty() 56 select p).ToArray(); 57 58 // 通过Linq进行left join, 得到数组B 59 var TD = 60 (from cItem in combine 61 join bItem in tdsd on cItem equals bItem into ps 62 from p in ps.DefaultIfEmpty() 63 select p).ToArray(); 64 65 JObject newkr = new JObject();//新——客人 66 JObject newtd = new JObject();//新——团队 67 JArray jkt = new JArray();//新—客人 68 JArray jtd = new JArray();//新——团队 69 string[] ku = new string[] { }; 70 for (int i = 0; i < Kre.Length; i++) 71 { 72 newkr = new JObject();//新——客人 73 ku = new string[] { }; 74 string j = Kre[i]; 75 if (string.IsNullOrEmpty(j)) 76 { 77 newkr.Add("krname", ""); 78 newkr.Add("krsex", ""); 79 newkr.Add("krmobile", ""); 80 newkr.Add("krsfz", ""); 81 } 82 else 83 { 84 ku = j.Split(','); 85 newkr.Add("krname", ku[0]); 86 newkr.Add("krsex", ku[1]); 87 newkr.Add("krmobile", ku[2]); 88 newkr.Add("krsfz", ku[3]); 89 } 90 jkt.Add(newkr); 91 } 92 string[] kutt = new string[] { }; 93 for (int k = 0; k < TD.Length; k++) 94 { 95 newtd = new JObject();//新——团队 96 kutt = new string[] { }; 97 string ty = TD[k]; 98 if (string.IsNullOrEmpty(ty)) 99 { 100 newtd.Add("tdname", ""); 101 newtd.Add("tdsex", ""); 102 newtd.Add("tdmobile", ""); 103 newtd.Add("tdsfz", ""); 104 105 } 106 else 107 { 108 kutt = ty.Split(','); 109 newtd.Add("tdname", kutt[0]); 110 newtd.Add("tdsex", kutt[1]); 111 newtd.Add("tdmobile", kutt[2]); 112 newtd.Add("tdsfz", kutt[3]); 113 } 114 jtd.Add(newtd); 115 } 116 jo.Add("ker", jkt); 117 jo.Add("td", jtd); 118 119 return jo.ToString(); 120 121 } 122 //分隔数组 123 public static string[] SplitArray(string[] Source, int StartIndex, int Len) 124 { 125 string[] result = new string[Len]; 126 for (int i = 0; i < Len; i++) 127 { 128 result[i] = i + StartIndex > Source.Length - 1 ? "" : Source[i + StartIndex]; 129 } 130 return result; 131 } 132 #endregion
<span>" + ker[w].krname + "</span>
<span>" + td[w].tdname+ "</span>
#region 客人名单对比 public string GetContrast(HttpContext context) { JObject jo = new JObject(); JObject jotd = new JObject(); JObject jakr = new JObject(); string[] arrkr = new string[] {}; #region string kerens = "";//客人名单 if (!string.IsNullOrEmpty(context.Request.Params["krres"])) { kerens = context.Request.Params["krres"].ToString().Trim(); if (kerens.Length > 0 && kerens != "") { arrkr = kerens.Split(','); } } string tdcz = "";//客人名单 string[] arrtd = new string[] { }; if (!string.IsNullOrEmpty(context.Request.Params["tdds"])) { tdcz = context.Request.Params["tdds"].ToString().Trim(); if (tdcz.Length > 0 && tdcz != "") { arrtd = tdcz.Split(','); } } List<string> arrkrsd = new List<string>(); string[] krstr = new string[] { }; for (int i = 0, j = 0; i < Math.Ceiling(arrkr.Length / 10m); i++, j = j + 10) { krstr = (SplitArray(arrkr, j, 10)); arrkrsd.Add(krstr[0] + "," + krstr[1] + "," + krstr[2] + "," + krstr[3] + "," + krstr[4] + "," + krstr[5] + "," + krstr[6] + "," + krstr[7] + "," + krstr[8] + "," + krstr[9]); arrkrsd.ToArray(); } var krsd = arrkrsd.Distinct().ToArray(); List<string> arrtdsd = new List<string>(); string[] tdstr = new string[] { }; for (int i = 0, j = 0; i < Math.Ceiling(arrtd.Length / 10m); i++, j = j + 10) { tdstr = (SplitArray(arrtd, j, 10)); arrtdsd.Add(tdstr[0] + "," + tdstr[1] + "," + tdstr[2] + "," + tdstr[3] + "," + tdstr[4] + "," + tdstr[5] + "," + tdstr[6] + "," + tdstr[7] + "," + tdstr[8] + "," + tdstr[9]); arrtdsd.ToArray(); } var tdsd=arrtdsd.Distinct().ToArray(); var combine = krsd.Concat(tdsd) // 合并 .Distinct() // 去掉重复项 .ToArray();
// 通过Linq进行left join, 得到数组A var Kre = (from cItem in combine join aItem in krsd on cItem equals aItem into ps from p in ps.DefaultIfEmpty() select p).ToArray();
// 通过Linq进行left join, 得到数组B var TD = (from cItem in combine join bItem in tdsd on cItem equals bItem into ps from p in ps.DefaultIfEmpty() select p).ToArray();
JObject newkr = new JObject();//新——客人 JObject newtd = new JObject();//新——团队 JArray jkt = new JArray();//新—客人 JArray jtd = new JArray();//新——团队 string[] ku = new string[] { }; for (int i = 0; i < Kre.Length; i++) { newkr = new JObject();//新——客人 ku = new string[] { }; string j = Kre[i]; if (string.IsNullOrEmpty(j)) { newkr.Add("krname", ""); newkr.Add("krsex", ""); newkr.Add("krmobile", ""); newkr.Add("krsfz", ""); newkr.Add("krvisa", ""); newkr.Add("krtypepass", ""); newkr.Add("kren", ""); newkr.Add("krcountry", ""); newkr.Add("krhit", ""); newkr.Add("krguestuu", "");
} else { ku = j.Split(','); newkr.Add("krname", ku[0]); newkr.Add("krsex", ku[1]); newkr.Add("krmobile", ku[2]); newkr.Add("krsfz", ku[3]); newkr.Add("krvisa", ku[4]); newkr.Add("krtypepass", ku[5]); newkr.Add("kren", ku[6]); newkr.Add("krcountry", ku[7]); newkr.Add("krhit", ku[8]); newkr.Add("krguestuu", ku[9]); } jkt.Add(newkr); } string[] kutt = new string[] { }; for (int k = 0; k < TD.Length; k++) { newtd = new JObject();//新——团队 kutt = new string[] { }; string ty = TD[k]; if (string.IsNullOrEmpty(ty)) { newtd.Add("tdname", ""); newtd.Add("tdsex", ""); newtd.Add("tdmobile", ""); newtd.Add("tdsfz", ""); newtd.Add("tdvisa", ""); newtd.Add("tdtypepass", ""); newtd.Add("tden", ""); newtd.Add("tdcountry", ""); newtd.Add("tdhit", ""); newtd.Add("tdguestuu", "");
} else { kutt = ty.Split(','); newtd.Add("tdname", kutt[0]); newtd.Add("tdsex", kutt[1]); newtd.Add("tdmobile", kutt[2]); newtd.Add("tdsfz", kutt[3]); newtd.Add("tdvisa", kutt[4]); newtd.Add("tdtypepass", kutt[5]); newtd.Add("tden", kutt[6]); newtd.Add("tdcountry", kutt[7]); newtd.Add("tdhit", kutt[8]); newtd.Add("tdguestuu", kutt[9]); } jtd.Add(newtd); } #endregion #region //string kr = ""; // string dt = ""; // string zo = ""; //int cont = (arrtdsd.Count) + (arrkrsd.Count); //string[] xarr = new string[cont]; //arrkrsd.CopyTo(xarr, 0); //arrtdsd.CopyTo(xarr, arrkrsd.Count);//将两个数组合并为一个新数组 //List<string> list = new List<string>(); //for (int i = 0; i < xarr.Length; i++)//删除重复元素 //{ // if (list.IndexOf(xarr[i].ToLower()) == -1) // list.Add(xarr[i]); //} //for (int i = 0; i < list.Count; i++) //{ // bool tl = false; // zo = list[i].ToString(); // int a = zo.IndexOf(","); // string li = ""; // li = zo.Substring(0, a); // if (arrkrsd.Count > arrtdsd.Count) // {
// for (int j = 0; j < arrkrsd.Count; j++) // { // kr = arrkrsd[j].ToString(); // int n = kr.IndexOf(","); // string krtt = ""; // krtt = kr.Substring(0, n); // if (li == krtt) // { // tl = true; // newkr.Add("" + i + "", arrkrsd[j]); // } // if (tl == false) // { // newkr.Add("" + i + "", "" + "," + "" + "," + "" + "," + "" + "," + "" + "," + "" + "," + "" + "," + "" + "," + "" + "," + ""); // } // } // for (int t = 0; t < arrtdsd.Count; t++) // { // dt = arrtdsd[t].ToString(); // int tt = dt.IndexOf(","); // string dttt = ""; // dttt = dt.Substring(0, tt); // if (kr == dttt) // { // tl = true; // newtd.Add("" + i + "", arrtdsd[t]); // } // if (tl == false) // { // newtd.Add("" + i + "", "" + "," + "" + "," + "" + "," + "" + "," + "" + "," + "" + "," + "" + "," + "" + "," + "" + "," + ""); // } // } // } // else // { // for (int j = 0; j < arrtdsd.Count; j++) // { // dt = arrtdsd[j].First().ToString(); // for (int t = 0; t < arrkrsd.Count; t++) // { // kr = arrkrsd[t].First().ToString(); // if (dt == kr) // { // tl = true; // newkr.Add(arrkrsd[t]); // newtd.Add(arrtdsd[j]); // } // } // if (tl == false) // { // newtd.Add(arrtdsd[j]); // }
// } // } //} #endregion jo.Add("ker", jkt); jo.Add("td", jtd); return jo.ToString();
} //分隔数组 public static string[] SplitArray(string[] Source, int StartIndex, int Len) { string[] result = new string[Len]; for (int i = 0; i < Len; i++) { result[i] = i + StartIndex > Source.Length - 1 ? "" : Source[i + StartIndex]; } return result; } #endregion