去重mongodb LIST
using MongoDB; using DockSample.DB; using MongoDB.Driver; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; using WeifenLuo.WinFormsUI.Docking; using MongoDB.Bson; namespace DockSample { public partial class Form2 : DockContent { public Form2() { InitializeComponent(); } ////////////////////// workaround of RichTextbox control‘s bug: ////////////////////// If load file before the control showed, all the text format will be lost ////////////////////// re-load the file after it get showed. ////////////////////private bool m_resetText = true; ////////////////////protected override void OnPaint(PaintEventArgs e) ////////////////////{ //////////////////// base.OnPaint(e); //////////////////// if (m_resetText) //////////////////// { //////////////////// m_resetText = false; //////////////////// } ////////////////////} ////////////////////////protected override string GetPersistString() ////////////////////////{ //////////////////////// // Add extra information into the persist string for this document //////////////////////// // so that it is available when deserialized. //////////////////////// return GetType().ToString() + ",1123," + Text; ////////////////////////} ////////////////////private void menuItem2_Click(object sender, System.EventArgs e) ////////////////////{ //////////////////// MessageBox.Show("This is to demostrate menu item has been successfully merged into the main form. Form Text=" + Text); ////////////////////} ////////////////////private void menuItemCheckTest_Click(object sender, System.EventArgs e) ////////////////////{ ////////////////////} ////////////////////protected override void OnTextChanged(EventArgs e) ////////////////////{ //////////////////// base.OnTextChanged(e); ////////////////////} private void button1_Click(object sender, EventArgs e) { usera u = new usera(); u.cid = 10; u.name = "测试看看"; WriteConcernResult s = MongoDBHelper.InsertOne("user", u); label1.Text = s.DocumentsAffected.ToString() + "------" + s.HasLastErrorMessage.ToString();//s.HasLastErrorMessage=false既是表示操作成功! } private void button2_Click(object sender, EventArgs e) { IMongoQuery query = MongoDB.Driver.Builders.Query.EQ("cid", 9); // usera u = MongoDBHelper.GetOne<usera>("user", query); IEnumerable<usera> u = MongoDBHelper.GetAll<usera>("user").Distinct<usera>(new ModelComparer()); //usera u = MongoDBHelper.GetOne<usera>("user", "555993c18825b905c8879edc"); //label1.Text = u.cid.ToString() + "---" + u.name; foreach(usera ua in u) { richTextBox1.AppendText(ua.cid.ToString() + "--" + ua.name + "\r\n"); } } private void button3_Click(object sender, EventArgs e) { MongoDBHelper.DeleteAll("user"); } } public class usera { public ObjectId id { get; set; } public int cid { get; set; } public string name { get; set; } } public class ModelComparer : IEqualityComparer<usera> { public bool Equals(usera x, usera y) { return x.name.ToUpper() == y.name.ToUpper(); } public int GetHashCode(usera obj) { return obj.name.ToUpper().GetHashCode(); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。