LINQ 按月分组
我无法获取按月份和年份分组的(亚音速)对象的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试我发现的这几个问题,但您基本上需要选择数据库对象还是匿名类型?
让我知道我的解决方案现在是否是您想要的。我还注意到你正在使用 Last?您使用的扩展程序我没有,所以我用 Max 替换了它。我没有安装 subsonic,所以它可能随库一起提供。
Try this couple issues i found, but you basically need to select a Database object versus anonymous type?
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.
无论如何,不要将查询语法中的 LINQ 和扩展方法语法中的 LINQ 组合起来。接下来使用:
Any way don't combine LINQ in query syntax and LINQ in extension methods syntax. Use next:
该问题显然与 Subsonic 解释某些 linq 语句的方式有关,并且是一个已知错误。
我通过声明 IEnumerable 类型的列表并使用 ToList() 来转换数据库交互来解决这个问题,最后查询被重新转换为 AsQueryable()
The issue is apparantly to do with the way Subsonic interprests certain linq statements and is a known bug.
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()