c# SortedList的妙用 (GroupBy)
生活随笔
收集整理的這篇文章主要介紹了
c# SortedList的妙用 (GroupBy)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
公司做一個藥品監控系統(web),目前負責對監控問題添加屏蔽規則功能。由于藥品問題要分成
問題級別 - 問題類型 - 問題具體結果類型 三層級別,以這三個級別,分成大組,再分成小組。由于本人煩于寫存儲過程,在sql 中使用語句頻繁查詢,性能又不好。于是想到利用
SortedList的自動排序和分組的功能,做了一個模板。覺得挺好用的,分享一下!
SortedList的自動排序和分組的功能,做了一個模板。覺得挺好用的,分享一下!
?? ??? 數據結構如下:
namespace CRMS.Web.Query.ProblemPrescription {public partial class ProblemDetail : CRMS.Web.Base.BasePage{// 由于datarow的構造函數是protected,這里巧妙地構造自定義table, 可以獲取自定義datarowprivate DataTable simpleTable = null;public DataTable SimpleTable{set { this.simpleTable = value; }get{if (simpleTable == null){simpleTable = new DataTable();System.Type stringType = System.Type.GetType("System.String");simpleTable.Columns.Add(new DataColumn("cli_lvl_code", stringType));simpleTable.Columns.Add(new DataColumn("cli_lvl_name", stringType));simpleTable.Columns.Add(new DataColumn("cli_title_code", stringType));simpleTable.Columns.Add(new DataColumn("cli_title_name", stringType));simpleTable.Columns.Add(new DataColumn("id", stringType)); // 以 "id" 代替 "cli_analyze_title"}return this.simpleTable;}}private result_data result_data;public result_data Result_data{set { this.result_data = value; }get { return this.result_data; }}public partial class result_data{private result_level[] result_levelField;public result_level[] result_level{get { return this.result_levelField; }set { this.result_levelField = value; }}private string nameField;[System.Xml.Serialization.XmlAttributeAttribute("name")]public string name{get { return this.nameField; }set { this.nameField = value; }}}public partial class result_level{private string nameField;[System.Xml.Serialization.XmlAttributeAttribute()]public string name{get { return this.nameField; }set { this.nameField = value; }}private result_title[] result_titleField;[System.Xml.Serialization.XmlElementAttribute("result_title", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]public result_title[] result_title{get { return this.result_titleField; }set { this.result_titleField = value; }}}public partial class result_title{private string nameField;public string name{get { return this.nameField; }set { this.nameField = value; }}private result[] resultField;public result[] result{get { return this.resultField; }set { this.resultField = value; }}}[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")][System.SerializableAttribute()][System.Diagnostics.DebuggerStepThroughAttribute()][System.ComponentModel.DesignerCategoryAttribute("code")][System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]public partial class result{private title titleFiled;[System.Xml.Serialization.XmlElementAttribute("title", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]public title Title{get { return this.titleFiled; }set { this.titleFiled = value; }}private detail detailFiled;[System.Xml.Serialization.XmlElementAttribute("detail", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]public detail Detail{get { return this.detailFiled; }set { this.detailFiled = value; }}private reference referenceFiled;[System.Xml.Serialization.XmlElementAttribute("reference", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]public reference Reference{get { return this.referenceFiled; }set { this.referenceFiled = value; }}public string mediA_hiscode;public string mediB_hiscode;public string result_type;public string result_id;}public partial class title{private string nameField;private string valueField;public string Name{get { return this.nameField; }set { this.nameField = value; }}public string Value{get { return this.valueField; }set { this.valueField = value; }}}public partial class detail{private string valueField;public string Value{get { return this.valueField; }set { this.valueField = value; }}}[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")][System.SerializableAttribute()][System.Diagnostics.DebuggerStepThroughAttribute()][System.ComponentModel.DesignerCategoryAttribute("code")][System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]public partial class reference{private string referenceField;[System.Xml.Serialization.XmlTextAttribute()]public string Value{get { return this.referenceField; }set { this.referenceField = value; }}}}// class ProblemDetail// 各個比較器和等價器public class lvl_title_id_comparer : IComparer<DataRow>{public int Compare(DataRow x, DataRow y){int res = string.Compare(x["cli_lvl_code"].ToString(), y["cli_lvl_code"].ToString());if (res != 0){return res;}else{res = string.Compare(x["cli_title_code"].ToString(), y["cli_title_code"].ToString());if (res != 0){return res;}else{return string.Compare(x["id"].ToString(), y["id"].ToString());}}}}public class lvl_title_comparer : IComparer<DataRow>{public int Compare(DataRow x, DataRow y){int res = string.Compare(x["cli_lvl_code"].ToString(), y["cli_lvl_code"].ToString());if (res != 0){return res;}else{return string.Compare(x["cli_title_code"].ToString(), y["cli_title_code"].ToString());}}}public class lvl_comparer : IComparer<DataRow>{public int Compare(DataRow x, DataRow y){return string.Compare(x["cli_lvl_code"].ToString(), y["cli_lvl_code"].ToString());}}public class lvl_title_equalitycomparer : IEqualityComparer<DataRow>{public bool Equals(DataRow x, DataRow y){return x["cli_lvl_code"].ToString() == y["cli_lvl_code"].ToString() &&x["cli_title_code"].ToString() == y["cli_title_code"].ToString();}public int GetHashCode(DataRow obj){return obj.ToString().GetHashCode();}}public class lvl_equalitycomparer : IEqualityComparer<DataRow>{public bool Equals(DataRow x, DataRow y){return x["cli_lvl_code"].ToString() == y["cli_lvl_code"].ToString();}public int GetHashCode(DataRow obj){return obj.ToString().GetHashCode();}} }
具體實現函數:
數據庫表結構:
pctd_analysis_result_detail_op 表
homh_user_analyze_rules_req 表
實現效果圖:
本次要屏蔽規則:
氨茶堿注射液≡≡維生素C注射液存在配伍禁忌
問題結果對應 result_data,
問題級別(重要警示,一般提示,其它信息)對應 result_level
問題標題(氨茶堿注射液≡≡維生素C注射液存在配伍禁忌)對應cli_title_name(cli_analyze_title, id 也可以)
在“屏蔽規則管理”中“批準”該屏蔽規則
批準屏蔽后,可以看到不會再顯示
氨茶堿注射液≡≡維生素C注射液存在配伍禁忌
參考例子:
http://msdn.microsoft.com/zh-cn/library/vstudio/bb534501.aspx
http://msdn.microsoft.com/zh-cn/library/vstudio/bb549393.aspx
class Pet{public string Name { get; set; }public double Age { get; set; }}public static void GroupByEx3(){// Create a list of pets.List<Pet> petsList =new List<Pet>{ new Pet { Name="Barley", Age=8.3 },new Pet { Name="Boots", Age=4.9 },new Pet { Name="Whiskers", Age=1.5 },new Pet { Name="Daisy", Age=4.3 } };// Group Pet objects by the Math.Floor of their age.// Then project an anonymous type from each group// that consists of the key, the count of the group's// elements, and the minimum and maximum age in the group.var query = petsList.GroupBy(pet => Math.Floor(pet.Age),(age, pets) => new{Key = age,Count = pets.Count(),Min = pets.Min(pet => pet.Age),Max = pets.Max(pet => pet.Age)});// Iterate over each anonymous type.foreach (var result in query){Console.WriteLine("\nAge group: " + result.Key);Console.WriteLine("Number of pets in this age group: " + result.Count);Console.WriteLine("Minimum age: " + result.Min);Console.WriteLine("Maximum age: " + result.Max);}/* This code produces the following output:Age group: 8Number of pets in this age group: 1Minimum age: 8.3Maximum age: 8.3Age group: 4Number of pets in this age group: 2Minimum age: 4.3Maximum age: 4.9Age group: 1Number of pets in this age group: 1Minimum age: 1.5Maximum age: 1.5*/}
總結
以上是生活随笔為你收集整理的c# SortedList的妙用 (GroupBy)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最高标号预留与推进算法 --- 就是要比
- 下一篇: 合并odex和少dex的apk为完整的a