20200807c#Linq查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestLinq
{
    public class Person
    {
        public int Id { set; get; }
        public String name { set; get; }
        public String Sex { set; get; }
        public int Money { set; get; }

        public Person(int id, String name, String sex,int money)
        {
            this.Id = id;
            this.name = name;
            this.Sex = sex;
            this.Money = money;
        }
        public Person()
        {

        }
        public override string ToString()
        {
            return Id + ":" + name + ":" + Sex;
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();


            List<Person> pList1 = new List<Person>();

            Person p  = new Person(1, "wuping3", "nv",1);
            pList1.Add(p);
            p = new Person(2, "wuping4", "nv",2);
            pList1.Add(p);
            p = new Person(3, "wuping5", "nv",3);
            pList1.Add(p);
            p = new Person(4, "wuping6", "nan",4);
            pList1.Add(p);
            p = new Person(5, "wuping7", "nan",5);
            pList1.Add(p);
            p = new Person(6, "wuping8", "nan",6);
            pList1.Add(p);
             



            //新的
            List<Person> pList2 = new List<Person>();
         
            p = new Person(0, "wuping3", "nv",7);
            pList2.Add(p);
            p = new Person(0, "wuping4", "nv",1);
            pList2.Add(p);
            p = new Person(0, "wuping5", "nv",2);
            pList2.Add(p);
             


            //t 就是新的,但是没有主键
            //需要更新的。
            var updateDb = from u in pList1
                            from t in pList2
                           where u.name == t.name && u.Sex == t.Sex
                            select new { ID = u.Id, t };
            List<Person> updateDbByID = new List<Person>();
            foreach (var item in updateDb)
            {

                item.t.Id = item.ID;//更新主键
                updateDbByID.Add(item.t);

                string key = item.t.name + "&" + item.t.Sex;
                if (!ht.Contains(key))
                {
                    ht.Add(key, item.t.Money);
                }
                else
                {
                    int oldMoney = (int)ht[key];
                    ht[key] = oldMoney > item.t.Money ? oldMoney : item.t.Money;

                }


            }
            foreach (Person item in updateDbByID)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("===========*****************************===========");
          
            //对数据库中的  进行遍历---》p
            //  只要 p 在集合 新的 就不要
            List<Person> insertIntoDb = pList1.Where(pq => !pList2.Any(p2 => p2.Id == pq.Id)).ToList<Person>();

            foreach (Person item in insertIntoDb)
            {
                Console.WriteLine(item);


                string key = item.name + "&" + item.Sex;
                if (!ht.Contains(key))
                {
                    ht.Add(key, item.Money);
                }
                else
                {
                    int oldMoney = (int)ht[key];
                    ht[key] = oldMoney > item.Money ? oldMoney : item.Money;

                }
            }
            Console.WriteLine("========需要更新的ZZ_SUM==========");
            foreach (string key in ht.Keys)
            {
                Console.WriteLine(key+":"+ht[key]);
            } 

            String updateIntoDBString = getUpdateSql(updateDbByID);
            Console.WriteLine(updateIntoDBString);

            Console.Read();
            
        }
        static string getUpdateSql(List<Person> pList)
        {
            string sqlUpdateString = "select * from Person where id in (@Id)";

            int[]array = pList.Select(p => p.Id).ToArray<int>();
 

            String ids = string.Concat("'",string.Join("','", array),"'");

            Console.WriteLine(ids.ToString());
            return sqlUpdateString.Replace("@Id", ids);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值