【问题标题】:Linq to sql select into a new classLinq to sql 选择进入一个新类
【发布时间】:2012-09-24 15:53:39
【问题描述】:

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

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

在此先感谢,并为我糟糕的解释感到抱歉。

【问题讨论】:

  • 第一个查询遇到什么问题?
  • 我已经更新了这个问题。在第一次查询中,g.Key 未填充 CampaignID 属性。

标签: c# entity-framework c#-4.0 linq-to-sql


【解决方案1】:

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

在你的课堂上:

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,必须采用不同的方法。

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

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 断开连接到实体,并允许您使用具有参数的构造函数创建所需的对象。有关详细信息,请参阅 MSDN 上的 LINQ to Entites 'parameterless constructor' error 论坛帖子。

您的另一个选择是使用您的 MSReport 类和对象初始化器语法进行查询,然后在您的类上有一个计算方法,您必须稍后调用。

【讨论】:

【解决方案2】:

这是一个例子......

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

其中AppModel.ShapeTypesEntity Framework 的实体。

【讨论】:

    【解决方案3】:

    也许默认构造函数在参数初始化之前运行? 尝试使用您的参数在 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
            }  
         }
    

    然后做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      相关资源
      最近更新 更多