zoukankan      html  css  js  c++  java
  • 往数据库添加的时候(只添加以前未添加的记录的写法)c#

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

    namespace ConsoleApplication1
    {
    public class Menu
    {
    public int Id { get; set; }
    public string Url { get; set; }
    public string Name { get; set; }
    public string ImageUrl { get; set; }

    }
    public class Favorites
    {
    public int Id { get; set; }
    public int MenuId { get; set; }
    public string MenuUrl{get;set;}
    public string MenuName{get;set;}

    }
    class Program
    {

    static void Main(string[] args)
    {
    //菜单初始化 start 这里相当于从数据库查询出来的记录
    List<Menu> MenuList = new List<Menu>();
    MenuList.Add(new Menu { Id=1,Url="baidu",Name="百度",ImageUrl="百度图片"});
    MenuList.Add(new Menu { Id = 2, Url = "google", Name = "谷歌", ImageUrl = "谷歌图片" });
    MenuList.Add(new Menu { Id = 3, Url = "souhu", Name = "搜狐", ImageUrl = "搜狐图片" });
    MenuList.Add(new Menu { Id = 4, Url = "sina", Name = "新浪", ImageUrl = "新浪图片" });
    MenuList.Add(new Menu { Id = 5, Url = "aiqiyi", Name = "爱奇艺", ImageUrl = "爱奇艺图片" });
    MenuList.Add(new Menu { Id = 6, Url = "bilibili", Name = "bilibili", ImageUrl = "bilibili图片" });
    ///菜单初始化 end
    //收藏夹初始化 start 这里相当于想要添加到数据的记录 只是要去除已经重复的记录
    List<Favorites> favoritesList = new List<Favorites>();
    favoritesList.Add(new Favorites { MenuId = 1, MenuUrl = "baidu", MenuName = "百度" });
    favoritesList.Add(new Favorites { MenuId = 2, MenuUrl = "google", MenuName = "谷歌" });
    favoritesList.Add(new Favorites { MenuId = 3, MenuUrl = "souhu", MenuName = "搜狐" });
    favoritesList.Add(new Favorites { MenuId = 4, MenuUrl = "sina", MenuName = "新浪" });
    favoritesList.Add(new Favorites { MenuId = 5, MenuUrl = "aiqiyi", MenuName = "爱奇艺" });
    favoritesList.Add(new Favorites { MenuId = 6, MenuUrl = "bilibili", MenuName = "bilibili" });
    favoritesList.Add(new Favorites { MenuId = 7, MenuUrl = "tengxun", MenuName = "腾讯" });
    favoritesList.Add(new Favorites { MenuId = 8, MenuUrl = "songxia", MenuName = "松下" });
    //收藏夹初始化 end
    #region 用于接收菜单中的选项 这里相当于已经存在的收藏
    List<Favorites> list = new List<Favorites>();
    list = MenuList.Select(m => new Favorites() { MenuId = m.Id, MenuUrl = m.Url }).ToList();
    #endregion
    //用于接收为收藏过的收藏
    List<Favorites> list2 = new List<Favorites>();
    //最新的收藏之中只要有一个收藏的MenuId,MenuUrl是在以前的收藏中出现过的就不是最新收藏 当b为true的时候为最新收藏放到list2中 定位最新收藏
    favoritesList.ForEach(f =>
    {
    bool b = true;
    list.ForEach(f1 => {
    if (f.MenuId == f1.MenuId && f.MenuUrl == f1.MenuUrl)
    {
    b = false;

    }

    }); //foreach end
    if (b)
    {
    list2.Add(f);
    }
    });


    }
    }
    }

  • 相关阅读:
    nginx 启动报错 “/var/run/nginx/nginx.pid" failed” 解决方法
    FastDFS+Nginx搭建Java分布式文件系统
    如何优雅使用Sublime Text3(Sublime设置豆沙绿背景色和自定义主题)
    HTTP请求/响应报文结构
    自学编程你得先看看这篇,你能收获很多
    年薪50W京东软件测试工程师的成长路——我们都曾一样迷茫
    学会Python除了不能生孩子,其他的都能做。
    面试题千变万化,为什么总是会问MySQL?
    要做有灵魂的程序员!!
    软件测试基础自学之测试基础理论,先看完这篇你再做测试
  • 原文地址:https://www.cnblogs.com/kexb/p/4638308.html
Copyright © 2011-2022 走看看