Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq联接表,按日期分组,值之和?_C#_Linq_Join_Group By_Sum - Fatal编程技术网

C# Linq联接表,按日期分组,值之和?

C# Linq联接表,按日期分组,值之和?,c#,linq,join,group-by,sum,C#,Linq,Join,Group By,Sum,我有两张桌子(一对多)仪表读数(0..1)和仪表读数详情(*) 我想加入这些表并按日期分组。日期字段以MeterReadings为单位,其他字段以MeterReadingDetails为单位 我使用了以下代码: Linq public static IEnumerable<MeterReadingsForChart> GetCustomerTotal(int CustomerId, int MeterTypeId, DateTime StartDate, DateTime EndDa

我有两张桌子(一对多)<代码>仪表读数(0..1)和仪表读数详情(*)

我想加入这些表并按日期分组。日期字段以MeterReadings为单位,其他字段以MeterReadingDetails为单位

我使用了以下代码:

Linq

public static IEnumerable<MeterReadingsForChart> GetCustomerTotal(int CustomerId, int MeterTypeId, DateTime StartDate, DateTime EndDate, MeterReadingsTimeIntervals DateRangeType)
{
    var customerReadings = from m in entity.MeterReadings
        join n in entity.MeterReadingDetails on m.sno equals n.ReadingId
        where m.Meters.CustomerId == CustomerId && m.ReadDate >= StartDate && m.ReadDate <= EndDate && m.Meters.TypeId == MeterTypeId
        group n by new { date = new DateTime(m.ReadDate.Value.Year, m.ReadDate.Value.Month, 1) } into g
        select new MeterReadingsForChart
        {
             ReadDate = g.Key.date,
             Value = g.Sum(x => x.Value),
             Name = g.FirstOrDefault().MeterReadingTypes.TypeName
         };

    return customerReadings;
}
但我有一个错误:

LINQ to实体中只支持无参数构造函数和初始值设定项

如何加入、分组和求和?

请尝试以下方法:

var customerReadings = (from m in entity.MeterReadings
    join n in entity.MeterReadingDetails on m.sno equals n.ReadingId
    where m.Meters.CustomerId == CustomerId && m.ReadDate >= StartDate && m.ReadDate <= EndDate && m.Meters.TypeId == MeterTypeId
    group n by new { Year = m.ReadDate.Value.Year, Month = m.ReadDate.Value.Month} into g
    select new
    {
         Key = g.Key,
         Value = g.Sum(x => x.Value),
         Name = g.FirstOrDefault().MeterReadingTypes.TypeName
     }).AsEnumerable()
       .Select(anon => new MeterReadingsForChart
       {
         ReadDate = new DateTime(anon.Key.Year, anon.Key.Month, 1),
         Value = anon.Value,
         Name = anon.Name
       });
var customerReadings=(来自entity.MeterReadings中的m
在m.sno上的entity.MeterReadingDetails中加入n等于n.ReadingId
其中m.Meters.CustomerId==CustomerId&&m.ReadDate>=StartDate&&m.ReadDate x.Value),
Name=g.FirstOrDefault().MeterReadingTypes.TypeName
}).可计算的()
.选择(anon=>new MeterReadingsForChart
{
ReadDate=新日期时间(anon.Key.Year,anon.Key.Month,1),
数值=非数值,
Name=anon.Name
});

Unf。它很难看,但实体框架不允许您创建DateTime(作为一个结构,它没有无参数构造函数)。因此,在这种情况下,我们希望从数据库中获得大部分结果,然后,随着数据流的流动,我们在内存中构建日期。

这与我的结果相同。结果也是一样的。@AliRızaAdıyahş很抱歉,你的意思是它给出了相同的错误吗?这段代码有效。非常感谢。但也有办法。第一步,我不费力地得到记录。然后将其转换为列表。第二步:按日期将列表分组。@AliRızaAdıyahş感谢您的反馈。在您提出的方案中,唯一需要注意的是,您在内存中分组,而不是在服务器上分组,因此对于大型数据集,可能会有性能问题issue@AliRızaAdıyahşi与大多数代码相关的东西一样,使用最合适的方法,我建议测试两者,如果没有真正的区别,使用最容易理解的方法
var customerReadings = (from m in entity.MeterReadings
    join n in entity.MeterReadingDetails on m.sno equals n.ReadingId
    where m.Meters.CustomerId == CustomerId && m.ReadDate >= StartDate && m.ReadDate <= EndDate && m.Meters.TypeId == MeterTypeId
    group n by new { Year = m.ReadDate.Value.Year, Month = m.ReadDate.Value.Month} into g
    select new
    {
         Key = g.Key,
         Value = g.Sum(x => x.Value),
         Name = g.FirstOrDefault().MeterReadingTypes.TypeName
     }).AsEnumerable()
       .Select(anon => new MeterReadingsForChart
       {
         ReadDate = new DateTime(anon.Key.Year, anon.Key.Month, 1),
         Value = anon.Value,
         Name = anon.Name
       });