private static int SetPictures() { var list = DB.Croisieres.Where(v => !v.Photos.Any()).ToList(); Console.WriteLine(list.Count); var listPic = Directory .GetFiles(_picDirectory) .Select(f => new FileInfo(f).Name) .ToList(); var stackPic = new Stack<string>(); foreach (var item in listPic) { stackPic.Push(item); } var cpt = 0; foreach (var croisieres in list) { if (stackPic.Count != 0) { cpt++; Console.Write(" {0} -", cpt); var photo = new Photos { PhotoName = stackPic.Pop(), IdPhoto = Guid.NewGuid() }; croisieres.Photos.Add(photo); if (cpt % 250 != 0) { continue; } cpt = 0; Console.WriteLine(); DB.SaveChanges(); } } DB.SaveChanges(); return list.Count; }
private static int SetPictures() { var list = DB.Croisieres.Where(v => !v.Photos.Any()).ToList(); Console.WriteLine(list.Count); var listPic = Directory .GetFiles(_picDirectory) .Select(f => new FileInfo(f).Name) .ToList(); //var stackPic = new Stack<string>(); int i = 1, j = 1; foreach (var croisieres in list) { j = 1; foreach (var item in listPic) { if (item == "Croisiere" + i + "_" + j + ".jpg") { var photo = new Photos { PhotoName = item, IdPhoto = Guid.NewGuid() }; croisieres.Photos.Add(photo); DB.SaveChanges(); Console.WriteLine("_" + item + "'" + croisieres.IdCroisiere); if (j <= 5) j++; } } if (i <= 20) i++; } Console.WriteLine(i); //var cpt = 0; //foreach (var croisieres in list) //{ // if (stackPic.Count != 0) // { // cpt++; // Console.Write(" {0} -", cpt); // var photo = new Photos // { // PhotoName = stackPic.Pop(), // IdPhoto = Guid.NewGuid() // }; // croisieres.Photos.Add(photo); // if (cpt % 250 != 0) // { // continue; // } // cpt = 0; // Console.WriteLine(); // DB.SaveChanges(); // } //} //DB.SaveChanges(); return list.Count; }