C# Linq to sql 选择成一个新类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12568587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq to sql select into a new class
提问by Cem
My problem is that when I have following 2 queries, 1st one is not populating the CampaignID property but 2nd one does. Here is my code;
我的问题是,当我有以下 2 个查询时,第一个没有填充 CampaignID 属性,但第二个会填充。这是我的代码;
query 1;
查询 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
};
query 2;
查询 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
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.
提前致谢,并为我糟糕的解释感到抱歉。
回答by gabba
Maybe defalt constructor runs before initialization of params? Try to add constructor in MSReport with your params and debug.
也许 defalt 构造函数在参数初始化之前运行?尝试使用参数在 MSReport 中添加构造函数并进行调试。
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 do:
并做:
select new MSReport(g.Key) {
StartDate = d1,
EndDate = d2
}
回答by JamieSee
When using the object initializer syntax, the values specified in the initializer are set after the constructor for the object is executed. If you need the values that you are trying to populate to be available to the constructor, you must add a form of the constructor that takes the values as arguments and populates the fields or properties itself.
使用对象初始值设定项语法时,初始值设定项中指定的值是在执行对象的构造函数之后设置的。如果您需要尝试填充的值对构造函数可用,则必须添加一种构造函数形式,该形式将值作为参数并填充字段或属性本身。
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 query:
在您的查询中:
new MSReport(g.Key, d1, d2)
This will work for Linq to SQL and Linq to Objects. For Linq to Entities a different approach must be taken.
这将适用于 Linq to SQL 和 Linq to Objects。对于 Linq to Entities,必须采取不同的方法。
You can execute the query with an anonymous object and then run a second query to transform it into your 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);
This disconnects the object from Linq to Entities and allows you to create your desired objects with a constructor that has parameters. See the LINQ to Entites 'parameterless constructor' errorforum post on MSDN for more information.
这将对象从 Linq 断开到实体,并允许您使用具有参数的构造函数创建所需的对象。有关详细信息,请参阅MSDN 上的LINQ to Entites 'parameterless constructor' 错误论坛帖子。
Your other option is to do the query using your MSReport class and the object initializer syntax then have a Calculate method on your class that you would have to call later.
您的另一个选择是使用您的 MSReport 类和对象初始值设定项语法进行查询,然后在您的类上使用Calculate 方法,稍后您必须调用该方法。
回答by Developer
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.ShapeTypesis entity of Entity Framework.
Entity FrameworkAppModel.ShapeTypes的实体在哪里 。



