往数据库添加的时候(只添加以前未添加的记录的写法)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);
}
});
}
}
}