EF copying entities with Navigation properties - c#

I have a requirement to copy objects along with its navigation properties and create new objects. Following is what I did:
// get parent location's work availability template
WorkAvailTemplate workAvailTemplate = locationsFromDb.Where(l => l.Location2 == null).SingleOrDefault().WorkAvailTemplates.First();
foreach (var loc in locationsToAddOrUpdate)
{
// add op-hours template only for the new locations (the ID sometimes can be -1
if (loc.LocationId < 1)
{
if (workAvailTemplate != null)
{
WorkAvailTemplate wt = new WorkAvailTemplate
{
Location = loc,
CreatedBy = this.CurrentUser.Id,
CreatedDate = DateTime.Now,
NumberOfRotations = workAvailTemplate.NumberOfRotations,
ActiveStatus = workAvailTemplate.ActiveStatus
};
ClientContext.WorkAvailTemplates.Add(wt);
var workAvailTemplateDays = workAvailTemplate.WorkAvailTemplateDays.ToList();
foreach (WorkAvailTemplateDay d in workAvailTemplateDays)
{
WorkAvailTemplateDay wd = new WorkAvailTemplateDay
{
WorkAvailTemplate = wt,
WeekNumber = d.WeekNumber,
DayNumber = d.DayNumber,
ChangedBy = this.CurrentUser.Id,
ChangedDate = DateTime.Now
};
ClientContext.WorkAvailTemplateDays.Add(wd);
foreach (var r in d.WorkAvailTemplateTimeRanges)
{
WorkAvailTemplateTimeRange wr = new WorkAvailTemplateTimeRange
{
WorkAvailTemplateDay = wd,
StartTime = r.StartTime,
EndTime = r.EndTime,
ChangedBy = this.CurrentUser.Id,
ChangedDate = DateTime.Now
};
ClientContext.WorkAvailTemplateTimeRanges.Add(wr);
}
}
}
}
}
I'm sure there's a better way to do this rather than creating new objects. Any thoughts?
Earlier, when I did this for a single Location object creation, i did as follow and it works fine.
WorkAvailTemplate workAvailTemplate = (from wat in ClientContext.WorkAvailTemplates
where wat.FkLocationId == newLocation.FkParentLocationId
select wat).SingleOrDefault();
if (workAvailTemplate != null)
{
workAvailTemplate.Location = newLocation;
ClientContext.WorkAvailTemplates.Add(workAvailTemplate);
var workAvailTemplateDays = from watd in ClientContext.WorkAvailTemplateDays
join wat in ClientContext.WorkAvailTemplates on watd.FkWorkAvailTemplateId equals wat.WorkAvailTemplateId
where wat.FkLocationId == newLocation.FkParentLocationId
select watd;
foreach (WorkAvailTemplateDay d in workAvailTemplateDays)
{
d.WorkAvailTemplate = workAvailTemplate;
var workAvailTimeRangePerDay = (from r in ClientContext.WorkAvailTemplateTimeRanges.AsEnumerable()
where r.FkWorkAvailTemplateDayId == d.WorkAvailTemplateDayId
select this.f(r, d, newLocation));
ClientContext.WorkAvailTemplateDays.Add(d);
ClientContext.WorkAvailTemplateTimeRanges.AddRange(workAvailTimeRangePerDay);
}
}
But when I did the same for mutiple objects in a loop, it only creates for the first Location object. I don't get any errors, but simply they don't get created for others.

Related

Why does my parallel.ForEach method return the wrong values?

I have a large List of objects (over 100k rows in a txt file). I have to do some data work with each item in the list I save the object to the database if it doesnt exist and if it does I increase a number column in the for the item and then update the database while saving the updated item.
If I use a regular foreach loop everything works out fine but it takes over 24 hours to finish.
The issue is that when I use a parallel loop the records do not match and sometimes I will get an error on the .Save() method that the item ID was changed.
THIS CODE WORKS
public List<SreckaIsplacena> UpisiUTabeleSerijski (List<SreckaTemp>srt)
{
_srecke = _glavniRepository.UcitajSamoaktivneSrecke().OrderByDescending(item => item.ID).ToList<Srecka>();
List<SreckaIsplacena> pomList = new List<SreckaIsplacena>();
SreckaIsplacena _isplacena;
foreach (SreckaTemp lt in srt)
{
string beznula = lt.sreIspBroj.TrimStart('0');
SreckeDobici srd = new SreckeDobici();
Srecka sr = (from s in _srecke
where s.Sifra == lt.sreSif && s.Serija == lt.sreSerija
select s).First();
List<SreckeDobici> srDob = _glavniRepository.DohvatiSreckeDobiciZaIsplatniBroj(sr, beznula);
if (srDob.Count == 1)
{
srd = srDob.ElementAt(0);
}
List<SreckaNagrade> sreckaNagrade = new List<SreckaNagrade>(_glavniRepository.DohvatiNagradeZaSrecku(sr.ID).OrderBy(item => item.SifraFox));
double iznos = lt.sreIznDob / lt.sreBrDob;
SreckaNagrade nag = (from sn in sreckaNagrade
where sn.Iznos == lt.sreIznDob
select sn).FirstOrDefault();
Odobrenje odo = new Odobrenje();
odo = odo.DohvatiOdobrenje(valutaGlavna.ID, lt.sreIsplatio).FirstOrDefault();
List<PorezSrecka> listaPoreza = _glavniRepository.UcitajPorezSrecka(valutaGlavna, odo, sr, nag.NagradaId);
List<SreckaIsplacena> sveIsplacene = _glavniRepository.DohvatiIsplaceneSreckeZaValutuIProdavacaSreckuNagraduNovo(valutaGlavna.ID, sr.ID, nag.NagradaId, lt.sreIsplatio);
if (sveIsplacene.Count > 1)
{
System.Windows.MessageBox.Show("Greska, ista srecka s istim dobitkom kod istog prodavaca u istoj valuti 2 put nadjena u bazi");
}
else if (sveIsplacene.Count == 1)
{
_isplacena = sveIsplacene.ElementAt(0);
_isplacena.BrojDobitaka = _isplacena.BrojDobitaka + lt.sreBrDob;
_isplacena.Update();
var index = pomList.FindIndex(r => r.ID == _isplacena.ID);
if (index != -1)
{
pomList[index] = _isplacena;
}
}
else if (sveIsplacene.Count==0)
{
_isplacena = new SreckaIsplacena();
decimal iz = Convert.ToDecimal(lt.sreIznDob);
_isplacena.BrojDobitaka = lt.sreBrDob;
_isplacena.Iznos = iz;
_isplacena.Nagrada = nag;
_isplacena.Prodavac = lt.sreIsplatio;
_isplacena.Valuta = valutaGlavna;
_isplacena.Srecka = sr;
_isplacena.Cijena = Convert.ToDecimal(sr.Cijena);
if (listaPoreza.Count == 1)
{
PorezSrecka ps = listaPoreza.ElementAt(0);
_isplacena.SreckaPorez = ps;
}
_isplacena.Save();
int ispID = _isplacena.ID;
if (ispID != 0)
{
srd.Sre_isplatio = lt.sreIsplatio;
srd.Sre_valuta = valutaGlavna;
srd.Update();
}
pomList.Add(_isplacena);
}
}
return pomList;
}
THIS CODE DOES NOT WORK
public List<SreckaIsplacena> UpisiSamoUTabele(ConcurrentBag<SreckaTemp> srt)
{
_srecke = _glavniRepository.UcitajSamoaktivneSrecke().OrderByDescending(item => item.ID).ToList<Srecka>();
List<SreckaIsplacena> pomList = new List<SreckaIsplacena>();
SreckaIsplacena _isplacena;
Parallel.ForEach(srt, (lt) =>
{
string beznula = lt.sreIspBroj.TrimStart('0');
SreckeDobici srd = new SreckeDobici();
Srecka sr = (from s in _srecke
where s.Sifra == lt.sreSif && s.Serija == lt.sreSerija
select s).First();
List<SreckeDobici> srDob = _glavniRepository.DohvatiSreckeDobiciZaIsplatniBroj(sr, beznula);
if (srDob.Count == 1)
{
srd = srDob.ElementAt(0);
}
List<SreckaNagrade> sreckaNagrade = new List<SreckaNagrade>(_glavniRepository.DohvatiNagradeZaSrecku(sr.ID).OrderBy(item => item.SifraFox));
SreckaNagrade nag = (from sn in sreckaNagrade
where sn.Iznos == lt.sreIznDob
select sn).FirstOrDefault();
Odobrenje odo = new Odobrenje();
odo = odo.DohvatiOdobrenje(valutaGlavna.ID, lt.sreIsplatio).FirstOrDefault();
List<PorezSrecka> listaPoreza = _glavniRepository.UcitajPorezSrecka(valutaGlavna, odo, sr, nag.NagradaId);
List<SreckaIsplacena> sveIsplacene = _glavniRepository.DohvatiIsplaceneSreckeZaValutuIProdavacaSreckuNagraduNovo(valutaGlavna.ID, sr.ID, nag.NagradaId, lt.sreIsplatio);
if (sveIsplacene.Count == 1)
{
_isplacena = sveIsplacene.ElementAt(0);
lock (_isplacena)
{
_isplacena.BrojDobitaka = _isplacena.BrojDobitaka + lt.sreBrDob;
_isplacena.Update();
var index = pomList.FindIndex(r => r.ID == _isplacena.ID);
if (index != -1)
{
pomList[index] = _isplacena;
}
}
}
else if (sveIsplacene.Count==0)
{
_isplacena = new SreckaIsplacena();
decimal iz = Convert.ToDecimal(lt.sreIznDob);
_isplacena.BrojDobitaka = lt.sreBrDob;
_isplacena.Iznos = iz;
_isplacena.Nagrada = nag;
_isplacena.Prodavac = lt.sreIsplatio;
_isplacena.Valuta = valutaGlavna;
_isplacena.Srecka = sr;
_isplacena.Cijena = Convert.ToDecimal(sr.Cijena);
lock(_isplacena)
{
if (listaPoreza.Count == 1)
{
PorezSrecka ps = listaPoreza.ElementAt(0);
_isplacena.SreckaPorez = ps;
}
_isplacena.Save();
int ispID = _isplacena.ID;
if (ispID != 0)
{
srd.Sre_isplatio = lt.sreIsplatio;
srd.Sre_valuta = valutaGlavna;
srd.Update();
}
pomList.Add(_isplacena);
}
}
});
return pomList;
}
I tried using ConcurrentBag instead of List but ran into an issue because ConcurrentBag does not have the FindIndex method (var index = pomList.FindIndex(r => r.ID == _isplacena.ID);). I know there is some way to take out the object and put back in but not sure how to do that and from what I read Lists should be ok if you use locking which I am.
Please help.

StringBuilder within IEnumerable

I have a ControlMeasure table that holds information on each control measure and a ControlMeasurepeopleExposed Table that holds a record for each person exposed in the control measure this could be 1 record or many records.
I Have a controller that populates a List view
For each item in the list, Control Measure, I would like to create a string that shows all the People at risk
e.g.
PeopleString = "Employees, Public, Others";
Ive added a foreach in the controller to show what I'm trying to do however I'm aware that this wont work.
The controller is this:
public ActionResult ControlMeasureList(int raId)
{
//Populate the list
var hazards = new List<Hazard>(db.Hazards);
var controlMeasures = new List<ControlMeasure>(db.ControlMeasures).Where(x => x.RiskAssessmentId == raId);
var cmcombined = (
from g in hazards
join f in controlMeasures
on new { g.HazardId } equals new { f.HazardId }
select new CMCombined
{
Activity = f.Activity,
ControlMeasureId = f.ControlMeasureId,
ExistingMeasure = f.ExistingMeasure,
HazardName = g.Name,
LikelihoodId = f.LikelihoodId,
Rating = f.Rating,
RiskAssessmentId = f.RiskAssessmentId,
SeverityId = f.SeverityId,
}).OrderBy(x => x.Activity).ToList();
var cmPeopleExp = new List<ControlMeasurePeopleExposed>(db.ControlMeasurePeopleExposeds).Where(x => x.RiskAssessmentId == raId);
var peopleExp = from c in cmPeopleExp
join d in db.PeopleExposeds
on c.PeopleExposedId equals d.PeopleExposedId
orderby d.Name
select new RAPeopleExp
{
RAPeopleExpId = c.PeopleExposedId,
PeopleExpId = c.PeopleExposedId,
PeopleExpName = d.Name,
RiskAssessmentId = c.RiskAssessmentId,
ControlMeasureId = c.ControlMeasureId
};
var model = cmcombined.Select(t => new FullControlMeasureListViewModel
{
ControlMeasureId = t.ControlMeasureId,
HazardName = t.HazardName,
LikelihoodId = t.LikelihoodId,
Rating = t.Rating,
SeverityId = t.SeverityId,
Activity = t.Activity,
ExCM = t.ExistingMeasure,
//This section here is where I'm struggling
var PeopleString = new StringBuilder();
foreach (var p in peopleExp)
{
PeopleString.AppendLine(p.PeopleName);
{
PeopleExposed = PeopleString,
});
return PartialView("_ControlMeasureList", model);
}
I know I cant directly put this code in the controller but it does represent what I want to do.
You can't foreach within an object initializer (which is what you're trying to do when instantiating FullControlMeasureListViewModel). You can, however, use a combination of string.Join and peopleExp.Select:
var model = cmcombined.Select(t => new FullControlMeasureListViewModel
{
//other props
PeopleExposed = string.Join(",", peopleExp
.Where(p => p.ControlMeasureId == t.ControlMeasureId)
.Select(p => p.PeopleExpName));
//other props
});

linq join, group by to get count of child table

After getting my join to work I seem to have gotten stuck on the count bit.
What I am attempting below is get a count of documents printed based on the join below.
What would the code be to get the count per 'guardiandocsrequired'?
var guardianEntityType = new {EntityTypeFK = "GUARDIAN"};
return (from d in dbContext.GuardianDocsRequireds
join p in dbContext.DocumentPrintingLogs on
new { docTypeFK = d.DocTypeFK, entityFK = d.GuardianFK } equals
new { docTypeFK = p.DocTypeFK, entityFK = p.EntityFK }
where d.GuardianFK == entityPK && p.ItemGroupFK == itemGroupID && p.EntityTypeFK == "GUARDIAN"
group d by new
{
d.GuardianFK,
d.DocTypeFK,
d.DocumentType.DocTypeDescription,
d.RequiredStatus
}
into res
select new DocumentsRequired
{
EntityPK = res.Key.GuardianFK,
EntityType = entityType,
DocTypeFK = res.Key.DocTypeFK,
DocTypeDescription = res.Key.DocTypeDescription,
RequiredStatus = res.Key.RequiredStatus,
PrintCount = ???
}
).ToList();
If it helps, I have written the sql to produce exactly what I require as follows:
SELECT gdr.DocRequiredID,gdr.RequiredDate,gdr.GuardianFK,gdr.DocTypeFK,gdr.RequiredStatus,
COUNT(dpl.DocPrintedID) AS documentsPrinted
FROM dbo.GuardianDocsRequired gdr
LEFT OUTER JOIN dbo.DocumentPrintingLog dpl ON gdr.DocTypeFK = dpl.DocTypeFK
AND gdr.GuardianFK = dpl.EntityFK
AND dpl.EntityTypeFK = 'GUARDIAN'
WHERE gdr.GuardianFK = #entityPK
GROUP BY gdr.DocRequiredID,gdr.RequiredDate,gdr.GuardianFK,gdr.DocTypeFK,gdr.RequiredStatus
Do you mean sth like this?
var guardiandocsrequired = (from d in dbContext.GuardianDocsRequireds
join p in dbContext.DocumentPrintingLogs on
new { docTypeFK = d.DocTypeFK, entityFK = d.GuardianFK } equals
new { docTypeFK = p.DocTypeFK, entityFK = p.EntityFK }
where d.GuardianFK == entityPK && p.ItemGroupFK == itemGroupID && p.EntityTypeFK == "GUARDIAN"
group d by new
{
d.GuardianFK,
d.DocTypeFK,
d.DocumentType.DocTypeDescription,
d.RequiredStatus
}
into res
select new DocumentsRequired
{
EntityPK = res.Key.GuardianFK,
EntityType = entityType,
DocTypeFK = res.Key.DocTypeFK,
DocTypeDescription = res.Key.DocTypeDescription,
RequiredStatus = res.Key.RequiredStatus,
PrintCount = ???
}
).ToList();
int cnt = guardiandocsrequired.Count;
return guardiandocsrequired;

Finding the date of an allday event in google calendar

I have a small C# program to read my Google Calender and return events.
public static IEnumerable<EventEntry> GetAllEvents(CalendarService service, DateTime? startData, DateTime? endData, int nbrToGet)
{
EventQuery query = new EventQuery(); // Create the query object:
if (startData != null)
{
query.StartTime = startData.Value;
}
if (endData != null)
{
query.EndTime = endData.Value;
}
query.SortOrder = CalendarSortOrder.ascending;
query.Uri = new Uri("https://www.google.com/calendar/feeds/" + service.Credentials.Username + "/private/full" + "?max-results=" + nbrToGet.ToString());
EventFeed calFeed = service.Query(query); // Tell the service to query:
return calFeed.Entries.Cast<EventEntry>();
}
I do a foreach on the returned events to display the event details:
foreach (var q in myevents)
{
var one = q.Title.Text;
var two = q.Times.ToList();
if (two.Count > 0)
{
a1 = two[0].StartTime.ToString();
a2 = two[0].EndTime.ToString();
}
else
{
a1 = string.Empty;
a2 = string.Empty;
}
var three = q.Content.Content;
test.Add(new MyCalendarData { Title = one, StartTime = a1, EndTime = a2, Content = three });
}
On all day events, there is no Start time or End time. Is there a way to find the date associated with the event?

C# Anonymous type foreach loop, a better way?

I have this simple foreach loop of an anonymous type and I'm wondering if there is a way to make it more efficient.
If it loops through 155 items, it takes about 20 seconds to complete. I've omitted some of the other properties it's setting on the boAsset object, but nothing special - just Strings/Integers.
Any thoughts?
List<BoAsset> assetList = new List<BoAsset>();
foreach (var asset in result)
{
BoAsset boAsset = new BoAsset();
boAsset.Description = asset.Description;
boAsset.DetailedDescription = asset.DetailedDescription;
boAsset.AssetCustomerID = asset.AssetCustomerID;
boAsset.AssetId = asset.AssetId;
boAsset.Keywords = asset.Keywords;
boAsset.Notes = asset.Notes;
boAsset.Photographer = asset.Photographer;
boAsset.PhotographerEmail = asset.PhotographerEmail;
boAsset.Notes = asset.Notes;
boAsset.Author = asset.Author;
boAsset.FileName = asset.FileName;
boAsset.FileExtension = asset.FileExtension;
boAsset.AssetCreateDate = asset.AssetCreateDate;
boAsset.AssetExpireDate = asset.AssetExpireDate;
assetList.Add(boAsset);
}
var query = (from a in context.Assets
join subAf1 in context.AssetFiles on new { aid = a.AssetID, ftid = 1 } equals new { aid = subAf1.AssetID, ftid = subAf1.AssetTypeID } into theAf1
from Af1 in theAf1.DefaultIfEmpty()
join subAf2 in context.AssetFiles on new { aid = a.AssetID, ftid = 2 } equals new { aid = subAf2.AssetID, ftid = subAf2.AssetTypeID } into theAf2
from Af2 in theAf2.DefaultIfEmpty()
join subAf3 in context.AssetFiles on new { aid = a.AssetID, ftid = 3 } equals new { aid = subAf3.AssetID, ftid = subAf3.AssetTypeID } into theAf3
from Af3 in theAf3.DefaultIfEmpty()
join subAf4 in context.AssetFiles on new { aid = a.AssetID, ftid = 4 } equals new { aid = subAf4.AssetID, ftid = subAf4.AssetTypeID } into theAf4
from Af4 in theAf4.DefaultIfEmpty()
join subAf5 in context.AssetFiles on new { aid = a.AssetID, ftid = 5 } equals new { aid = subAf5.AssetID, ftid = subAf5.AssetTypeID } into theAf5
from Af5 in theAf5.DefaultIfEmpty()
join subFp in context.FilePaths on new { fpid = Af1.FilePathID } equals new { fpid = subFp.FilePathID } into theFp1
from fp1 in theFp1.DefaultIfEmpty()
//join fp in context.FilePaths on Af1.FilePathID equals fp.FilePathID
where a.AssetCustomerID == custId && a.AssetID == assetId
select new { a, Af1, Af2, Af3, Af4, Af5, fp1 }).Distinct();
var result = from q in query
select new
{
AssetId = q.a.AssetID,
AssetCustomerId = q.a.AssetCustomerID,
StockImage = q.a.StockImage,
Description = q.a.Description,
DetailedDescription = q.a.DetailedDescription,
Author = q.a.Author,
FileName = q.Af1.FileName, //was 1
FileExtension = q.Af1.FileExtension, //was 1
AssetCreateDate = q.a.AssetCreateDate,
AssetExpireDate = q.a.AssetExpireDate,
AssetActivateDate = q.a.AssetActivateDate,
Notes = q.a.Notes,
Keywords = q.a.Keywords,
Photographer = q.a.Photographer,
PhotographerEmail = q.a.PhotographerEmail
}
The time for creating 155 objects and copying the data into them would be something like a millisecond. There is nothing that you could do to that code to make any significant (or even noticable) improvement.
If you want to save any time, you should look at the query that creates the result. That's what's taking time.
I think that your query is far too complex (too many joins), especially given that there are a few that are not even used.
Note that some LINQ providers (such as LINQ to Entities) can take quite a bit of time in simply processing a query tree to transform it into SQL, if the query is complex. (I have already had some queries take 10 or 15 seconds just for EF to analyze it.)
If, as I suspect, the LINQ implementation that you are using is hitting a database, then use a SQL profiling tool in order to check what actual SQL is being passed to the server, and how performant that SQL is.
Also, note that given the fields you are using, you could actually do away with the foreach loop by materializing results directly into the BoAsset objects.
That is:
var result = from q in query
select new BoAsset
{
AssetId = q.a.AssetID,
AssetCustomerId = q.a.AssetCustomerID,
StockImage = q.a.StockImage,
Description = q.a.Description,
DetailedDescription = q.a.DetailedDescription,
Author = q.a.Author,
FileName = q.Af1.FileName, //was 1
FileExtension = q.Af1.FileExtension, //was 1
AssetCreateDate = q.a.AssetCreateDate,
AssetExpireDate = q.a.AssetExpireDate,
AssetActivateDate = q.a.AssetActivateDate,
Notes = q.a.Notes,
Keywords = q.a.Keywords,
Photographer = q.a.Photographer,
PhotographerEmail = q.a.PhotographerEmail
};
List<BoAsset> assetList = result.ToList();

Categories

Resources