Linq to sql选择进入一个新类 [英] Linq to sql select into a new class

查看:177
本文介绍了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.    
    }  
 }

提前感谢,

推荐答案

当使用对象初始化语法时,初始化器中指定的值将在执行对象的构造函数之后设置。如果您需要想要填充的值以供构造函数使用,则必须添加一个构造函数形式,该值将这些值作为参数,并填充字段或属性本身。

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到SQL和Linq到对象。对于Linq to Entities,必须采用不同的方法。

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

您可以使用匿名对象执行查询,然后运行第二个查询将其转换为所需的对象: / p>

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'无参构造函数'错误<

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选择进入一个新类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆