本文介绍了两个列表同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我做的两个列表之间的同步。 的IList<事件> GoogleEvents 和表< myEvent> DB.Events;I'm doing the synchronization between the two lists.IList<Event> GoogleEvents and Table<myEvent> DB.Events;在谷歌方面我使用的这个String Summary,String Description,EventDateTime Start,EventDateTime End,Event.RemindersData Reminders;在数据库端像这样我的代码是这样的。foreach (myEvent item in DB.Events){ if (item.GoogleID == "" || item.GoogleID == null)// i add event my db { //Add dbEvent to google and save id to googleid column }}foreach (Event item in myGoogleCalendar.Items){ if (DB.Events.Where(o => o.GoogleID == item.Id).Count() == 0)// i add event google { //Add googleEvent to dbEvent } else { //This event also existing in two server myEvent dbEvent = DB.Olaylar.Where(o => o.GoogleID == item.Id).First(); if(item.Updated.Value == dbEvent.UpdateTime) continue;//Same if (item.Updated.Value > dbEvent.UpdateTime) { //Google event is new } else { //dbEvent is new } }}的有一个很大的问题的我忘了删除我应该在哪里添加删除活动的一部分,怎么样?Where should I add deleting events part and how ?推荐答案的问题是不执行删除同步,但认识到,该事件是否已在一个列表中被删除或添加到其他列表The problem is not to implement a delete synchronization, but to recognize, whether the event has been deleted in one list or added to the other list.我建议不要删除从DB.Events列表中的事件,而是将它们标记为删除。I recommend not to delete events from the DB.Events list, but to mark them as deleted.因此,你需要一个标志bool IsDeleted = false;在您键入 myEvent 。在谷歌列表(我假设你使用的 https://developers.google.com/google-apps/calendar/v3/reference/events ),你没有这样的标志。所以,你的算法需要有点棘手。In Google list (I assume you use https://developers.google.com/google-apps/calendar/v3/reference/events) you don't have such a flag. So your algorithm needs to be a bit tricky.所以,如果你在谷歌列表需要首先同步So you need the first synchronize from the Google listforeach (Event item in myGoogleCalendar.Items){ if (DB.Events.Where(o => o.GoogleID == item.Id).Count() == 0) { // create new DB event from Google event, because since it is // not in the DB.Events list, it has not been deleted there earlier, // else there would be an event with IsDeleted=true } else { //This event exists in both myEvent dbEvent = DB.Events.Where(o => o.GoogleID == item.Id).First(); if (dbEvent.IsDeleted) { // delete Google event, since the flag in DB.Events list shows that // it existed earlier and has been deleted in DB.Events list. } else { if(item.Updated.Value == dbEvent.UpdateTime) continue;//Same if (item.Updated.Value > dbEvent.UpdateTime) { // update DB event } else { // update Google event } } }}然后你从同步DB.Events列表Then your synchronize from your DB.Events listforeach (myEvent item in DB.Events){ if (!item.IsDeleted && string.IsNullOrEmpty(item.GoogleID)) { // Create new Google Event from DB event } else if (!item.IsDeleted && !string.IsNullOrEmpty(item.GoogleID)) { // Mark DB event as deleted item.IsDeleted = true; }}这工作在场景 您创建DB事件 您创建谷歌事件 您可以在数据库中删除事件 您在谷歌删除事件You create an event in DBYou create an event in GoogleYou delete an event in DBYou delete an event in Google 这篇关于两个列表同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 06:09