Linq to sql select a new class

My problem is that when I have 2 requests, the first one does not populate the CampaignID property, and the second one does. Here is my code;

request 1 ;

var query = from c in _context.MCTargets where c.TargetDateFrom==d1 && c.TargetDateTo<=d2 group c by c.MarketingCampaignID into g select new MSReport{ CampaignID = g.Key, // CampaignID is not populated here. StartDate = d1, EndDate = d2 }; 

request 2 ;

 var query2 = from c in _context.MCTargets where c.TargetDateFrom == d1 && c.TargetDateTo <= d2 group c by c.MarketingCampaignID into g select new { CampaignID = g.Key, StartDate = d1, EndDate = d2 }; 

MSReport.cs

 public class MSReport { public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public int CampaignID { get; set; } public MSReport() { // CampaignID = 0 here // doing something with CampaignID here like setting some calculated properties. } } 

Thanks in advance and sorry for my poor explanation.

+4
source share
3 answers

When using the object initializer syntax, the values ​​specified in the initializer are set after the constructor is executed for the object. If you need values ​​that you want to fill in order to be accessible to the constructor, you must add a constructor form that takes values ​​as arguments and populates fields or properties.

In your class:

 public MSReport(int campaignID, DateTime startDate, DateTime endDate) { CampaignID = campaignID; StartDate = startDate; EndDate = endDate; // doing something with CampaignID here like setting some calculated properties. } 

In your request:

 new MSReport(g.Key, d1, d2) 

This will work for Linq to SQL and Linq for objects. For Linq to Entities, a different approach should be used.

You can execute the request with an anonymous object, and then run the second request to convert it to the desired object:

 var query = from c in _context.MCTargets where c.TargetDateFrom==d1 && c.TargetDateTo<=d2 group c by c.MarketingCampaignID into g select new { CampaignID = g.Key, StartDate = d1, EndDate = d2 }; IEnumerable<MSReport> queryMSReports = from item in query.AsEnumerable() select new MSReport(item.CampaignID, item.StartDate, item.EndDate); 

Disconnects an object from Linq to Entities and allows you to create the necessary objects using the constructor with parameters. For more information, see the LINQ to Entites "no-parameter constructor" error message in MSDN for more information.

Another option is to execute the request using the MSReport class and object initializer syntax, and then use the Calculate method for your class, which you will need to call later.

+11
source

Maybe the defalt constructor works before parameter initialization? Try adding a constructor to MSReport with your parameters and debugging.

  public class MSReport { public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public int CampaignID { get; set; } public MSReport(int campaginId, ....) { // use and initialize camaginId here } } 

and execute:

 select new MSReport(g.Key) { StartDate = d1, EndDate = d2 } 
0
source

Here is an example ....

  public class SimpleNameValueItem { public string Name { get; set; } public Guid Uid { get; set; } public int Id { get; set; } public string Value { get; set; } } var shapeItems = from x in AppModel.ShapeTypes select new SimpleNameValueItem { Name = x.ShapeName, Uid = x.UID }; 

Where AppModel.ShapeTypes is an Entity Framework object.

enter image description here

0
source

Source: https://habr.com/ru/post/1435965/


All Articles