예제 #1
0
        public async void UpdateData(StructDoc structDoc)
        {
            try
            {
                DDB dB = new DDB();

                var Lss = dB.site_source.Where(t => t.NameSite == structDoc.MainLink).ToList();
                if (!Lss.Any())
                {
                    dB.site_source.Add(new site_source()
                    {
                        NameSite = structDoc.MainLink
                    });
                    await dB.SaveChangesAsync();
                }
                int ine = 0;
                foreach (var item in structDoc.LItemDoc)
                {
                    var a1 = dB.NewsOnSites.Where(t => t.C_guid == item.guid).Any();
                    if (!a1)
                    {
                        NewsOnSites newsOn = new NewsOnSites();
                        newsOn.Title       = item.Title;
                        newsOn.pudData     = item.ItemDateTime;
                        newsOn.link        = item.link;
                        newsOn.discription = item.discription;
                        newsOn.C_guid      = item.guid;
                        dB.NewsOnSites.Add(newsOn);
                        ine += await dB.SaveChangesAsync();

                        foreach (var cat in item.Categori)
                        {
                            var         one_cat = dB.category.Where(t => t.NameCategory == cat).ToList();
                            site_source site    = new site_source();
                            site = dB.site_source.Where(t => t.NameSite == structDoc.MainLink).First();
                            if (!one_cat.Any())
                            {
                                dB.category.Add(new category()
                                {
                                    NameCategory = cat, SiteWithCategory = site.IdTable
                                });
                                await dB.SaveChangesAsync();
                            }

                            var idNOS = dB.NewsOnSites.Where(t => t.C_guid == item.guid).First().C_guid;
                            var idCat = dB.category.Where(t => t.NameCategory == cat && t.SiteWithCategory == site.IdTable).First().IdTable;

                            dB.NewsAndCategory.Add(new NewsAndCategory()
                            {
                                IdCategory = idCat, IdNews = idNOS
                            });
                            await dB.SaveChangesAsync();
                        }
                    }
                }
                Console.WriteLine(structDoc.MainLink);
                Console.WriteLine("Прочитанно новочтей: " + structDoc.LItemDoc.Count);
                Console.WriteLine("Сохранено новостей: " + ine);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }