public int DumpNewEnodebExcels(IEnumerable <ENodebExcel> infos) { var containers = (from info in infos join town in _townRepository.GetAllList() on new { info.CityName, info.DistrictName, info.TownName } equals new { town.CityName, town.DistrictName, town.TownName } select new ENodebExcelWithTownIdContainer { ENodebExcel = info, TownId = town.Id }).ToArray(); if (!containers.Any()) { return(0); } var items = Mapper.Map <IEnumerable <ENodebExcelWithTownIdContainer>, List <ENodebWithTownIdContainer> >(containers); items.ForEach(x => { x.ENodeb.TownId = x.TownId; }); var count = 0; foreach (var eNodeb in items.Select(x => x.ENodeb).ToList()) { var item = _eNodebRepository.FirstOrDefault(x => x.ENodebId == eNodeb.ENodebId); if (item == null) { var result = _eNodebRepository.Insert(eNodeb); if (result != null) { count++; } } else { item.IsInUse = true; _eNodebRepository.Update(item); } } _eNodebRepository.SaveChanges(); return(count); }