LINQ Error with DTO
I'm seeing a weird issue and I think I'm missing something. The DTO doesn't seem to get populated when returned. Only the properties that are being init in the LINQ query get set, the ones that are being set in ParseJobResultsXml do not get setup as seen in the DEBUG output.
The DTO is being setup in a LINQ query, something like this:
public class JobResultDTO
{
[Key]
public string Id { get; set; }
public string Created { get; set; }
public string Finished { get; set; }
public string Status { get; set; }
public string PlantLink { get; set; }
public IEnumerable<string> Messages { get; set; }
public string JobType { get; set; }
}
private void ParseJobResultXml(string jobResultXml, JobResultDTO jobDto)
{
try
{
var xmlElement = XElement.Parse(jobResultXml);
if (xmlElement != null)
{
jobDto.Finished = xmlElement.Element("Date").Value;
jobDto.Status = xmlElement.Element("Status").Value;
jobDto.PlantLink = xmlElement.Element("PlantLink").Value;
jobDto.Messages = xmlElement.Element("Messages").Elements("Message").Select(m => m.Value);
}
}
catch { }
}
var jobsAndResults = _context.Jobs.Where(j => j.JobType == jobOpenPlant || j.JobType == jobNormSite)
.AsEnumerable()
.Where(j =&g开发者_JAVA技巧t; JobResultXmlHelper.JobBelongsToUser(j.JobResult, userLogin))
.OrderByDescending(j => j.JobCreated)
.Select(j => new
{
Result = j.JobResult,
Dto = new JobResultDTO
{
Id = j.Id.ToString(),
JobType = j.JobType,
Created = (j.JobCreated ?? DateTime.Now).ToString()
}
});
foreach (var j in jobsAndResults)
{
ParseJobResultXml(j.Result, j.Dto);
DumpDTO(j.Dto); //I see it set up correctly here
}
jobs.AddRange(jobsAndResults.Select(j => j.Dto));
DumpDTO(jobs.ElementAt(0)); //Now only the Key property is set
return jobs;
This is Debug output I'm seeing on the server for the two DEBUG lines
On Server... Id: 51a8d041-5dff-4849-9651-9fb2fe89816a Status: Finished
Catalog - Updated 0 record(s) successfully: Model - Updated 0 record(s) successfully:
On Server... Id: 51a8d041-5dff-4849-9651-9fb2fe89816a Status:
As you can see the 2nd one has no entry for Status coln. Any ideas why this is happening?
I think your code is correct, but you don't have waiting time.
I didn't see any LoadOperation in your syntax.
Your code will work in WPF, but not in SilverLight.
*Update:*Try
foreach (var j in jobsAndResults)
{
ParseJobResultXml(j.Result, j.Dto);
DumpDTO(j.Dto); //I see it set up correctly here
jobs.Add(j.Dto);
}
精彩评论