zoukankan      html  css  js  c++  java
  • 去重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 + "
    ");
                }
            }
    
            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();
            }
        }
    }
    

      

  • 相关阅读:
    css样式兼容不同浏览器问题解决办法
    css 中input和select混排对齐问题
    盒模型详解
    css中的width,height,属性与盒模型的关系
    php中将文中关键词高亮显示,快捷方式可以用正则
    数据库面试知识
    ConcurrentHashMap原理分析(1.7与1.8)
    Docker 学习笔记
    秒杀系统架构分析与实战
    spring + redis 实现数据的缓存
  • 原文地址:https://www.cnblogs.com/wangchuang/p/4513965.html
Copyright © 2011-2022 走看看