Linq to sql 选择成一个新类

时间:2023-03-08
本文介绍了Linq to sql 选择成一个新类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,当我有以下 2 个查询时,第一个没有填充 CampaignID 属性,但第二个会填充.这是我的代码;

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;

查询 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
                    };

查询 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.

推荐答案

当使用对象初始值设定项语法时,初始值设定项中指定的值是在执行对象的构造函数之后设置的.如果您需要尝试填充的值对构造函数可用,则必须添加一种构造函数形式,该形式将值作为参数并填充字段或属性本身.

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.

在你的课堂上:

public MSReport(int campaignID, DateTime startDate, DateTime endDate)
{
       CampaignID = campaignID;
       StartDate = startDate;
       EndDate = endDate;

       // doing something with CampaignID here like setting some calculated properties.
}     

在您的查询中:

new MSReport(g.Key, d1, d2)

这适用于 Linq to SQL 和 Linq to Objects.对于 Linq to Entities,必须采用不同的方法.

This will work for Linq to SQL and Linq to Objects. For Linq to Entities a different approach must be taken.

您可以使用匿名对象执行查询,然后运行第二个查询以将其转换为您想要的对象:

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);

这会断开对象从 Linq 到实体的连接,并允许您使用具有参数的构造函数创建所需的对象.请参阅 LINQ to Entites 'parameterless构造函数错误 MSDN 上的论坛帖子以获取更多信息.

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' error forum post on MSDN for more information.

您的另一个选择是使用您的 MSReport 类和对象初始值设定项语法进行查询,然后在您的类上使用Calculate 方法,稍后您必须调用该方法.

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.

这篇关于Linq to sql 选择成一个新类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:LINQ 有多快? 下一篇:如何让 Linq to SQL 识别动态存储过程的结果集?

相关文章

最新文章