LINQ 按月分组

发布于 2024-09-28 08:02:10 字数 1473 浏览 2 评论 0原文

我无法获取按月份和年份分组的(亚音速)对象的 IQueryable 列表。

对象的基本视图...

public partial class DatabaseObject
{
    [SubSonicPrimaryKey]
    public int objectID { get; set; }

    public string Description { get; set; }

    public decimal Value { get; set; }

    public string Category { get; set; }

    public DateTime DateOccurred { get; set; }
}

在我的数据库存储库中获取 IQueryable 的方法...

public IQueryable GetData(string DataType)
{
   return (from t in db.All<DatabaseObject>()
           orderby t.DateOccurred descending
           select t)
          .Where(e => e.Category == DataType);  
}

我的问题是,如何返回按月分组的日期?我已经尝试过以下方法,但这会导致编译器发出有关匿名类型的警告...

public IQueryable GetData(string DataType)
{
   var datalist = (from t in db.All<FinancialTransaction>().Where(e => e.Category == DataType);
                   let m = new
                   {
                       month = t.DateOccurred.Month,
                       year = t.DateOccurred.Year
                   }
                   group t by m into l select new
                   {
                       Description = string.Format("{0}/{1}", l.Key.month, l.Key.year),
                       Value = l.Sum(v => v.Value), // Sum(v => v.Value),
                       Category = "Grouped"
                       DateOccurred = l.Last(v => v.DateOccurred)
                   }
    return datalist;
}

有什么想法吗?

I am having trouble getting an IQueryable list of a (subsonic) object grouped by Month and Year.

Basic view of the object...

public partial class DatabaseObject
{
    [SubSonicPrimaryKey]
    public int objectID { get; set; }

    public string Description { get; set; }

    public decimal Value { get; set; }

    public string Category { get; set; }

    public DateTime DateOccurred { get; set; }
}

Method to get IQueryable in my Database repository...

public IQueryable GetData(string DataType)
{
   return (from t in db.All<DatabaseObject>()
           orderby t.DateOccurred descending
           select t)
          .Where(e => e.Category == DataType);  
}

My question is, how can I return the dates grouped by Month? I have tried the below, but this results in compiler warnings regarding anonymous types...

public IQueryable GetData(string DataType)
{
   var datalist = (from t in db.All<FinancialTransaction>().Where(e => e.Category == DataType);
                   let m = new
                   {
                       month = t.DateOccurred.Month,
                       year = t.DateOccurred.Year
                   }
                   group t by m into l select new
                   {
                       Description = string.Format("{0}/{1}", l.Key.month, l.Key.year),
                       Value = l.Sum(v => v.Value), // Sum(v => v.Value),
                       Category = "Grouped"
                       DateOccurred = l.Last(v => v.DateOccurred)
                   }
    return datalist;
}

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍景流年 2024-10-05 08:02:10

尝试我发现的这几个问题,但您基本上需要选择数据库对象还是匿名类型?

IQueryable<DatabaseObject> datalist = (
from t in db.All<FinancialTransaction>().Where(e => e.Category == DataType)
let m = new
{
    month = t.DateOccurred.Month,
    year = t.DateOccurred.Year
}
group t by m into l 
select new DatabaseObject()
{
    Description = string.Format("{0}/{1}", l.Key.month, l.Key.year),
    Value = l.Sum(v => v.Value),   //Sum(v => v.Value),
    Category = "Grouped", 
    DateOccurred = l.Max(v => v.DateOccurred)
}).AsQueryable();

让我知道我的解决方案现在是否是您想要的。我还注意到你正在使用 Last?您使用的扩展程序我没有,所以我用 Max 替换了它。我没有安装 subsonic,所以它可能随库一起提供。

Try this couple issues i found, but you basically need to select a Database object versus anonymous type?

IQueryable<DatabaseObject> datalist = (
from t in db.All<FinancialTransaction>().Where(e => e.Category == DataType)
let m = new
{
    month = t.DateOccurred.Month,
    year = t.DateOccurred.Year
}
group t by m into l 
select new DatabaseObject()
{
    Description = string.Format("{0}/{1}", l.Key.month, l.Key.year),
    Value = l.Sum(v => v.Value),   //Sum(v => v.Value),
    Category = "Grouped", 
    DateOccurred = l.Max(v => v.DateOccurred)
}).AsQueryable();

Let me know if my solution is now what you want. I also noticed you were using Last? The extension you were using I do not have so I replaced it with Max. I don't have subsonic installed so it might come with the libraries.

风吹过旳痕迹 2024-10-05 08:02:10

无论如何,不​​要将查询语法中的 LINQ 和扩展方法语法中的 LINQ 组合起来。接下来使用:

from t in db.All<DatabaseObject>()    
where e.Category equals DataType
orderby t.DateOccurred descending
select t;

Any way don't combine LINQ in query syntax and LINQ in extension methods syntax. Use next:

from t in db.All<DatabaseObject>()    
where e.Category equals DataType
orderby t.DateOccurred descending
select t;
对你的占有欲 2024-10-05 08:02:10

该问题显然与 Subsonic 解释某些 linq 语句的方式有关,并且是一个已知错误

IEnumerable<DatabaseObject> datalist = (
from t in db.All<FinancialTransaction>().Where(e => e.Category == DataType).ToList()
let m = new
{
    month = t.DateOccurred.Month,
    year = t.DateOccurred.Year
}
group t by m into l 
select new DatabaseObject()
{
    Description = string.Format("{0}/{1}", l.Key.month, l.Key.year),
    Value = l.Sum(v => v.Value),   //Sum(v => v.Value),
    Category = "Grouped", 
    DateOccurred = l.Max(v => v.DateOccurred)
}).AsQueryable();

我通过声明 IEnumerable 类型的列表并使用 ToList() 来转换数据库交互来解决这个问题,最后查询被重新转换为 AsQueryable()

The issue is apparantly to do with the way Subsonic interprests certain linq statements and is a known bug.

IEnumerable<DatabaseObject> datalist = (
from t in db.All<FinancialTransaction>().Where(e => e.Category == DataType).ToList()
let m = new
{
    month = t.DateOccurred.Month,
    year = t.DateOccurred.Year
}
group t by m into l 
select new DatabaseObject()
{
    Description = string.Format("{0}/{1}", l.Key.month, l.Key.year),
    Value = l.Sum(v => v.Value),   //Sum(v => v.Value),
    Category = "Grouped", 
    DateOccurred = l.Max(v => v.DateOccurred)
}).AsQueryable();

I have fixed this by declaring an list of type IEnumerable and using ToList() to cast the database interaction, finally the query is then recast AsQueryable()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文