我无法获得按月和年分组的(亚音速)对象的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;
}
有什么想法吗?
答案 0 :(得分:1)
尝试我发现的这几个问题,但你基本上需要选择一个数据库对象而不是匿名类型?
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替换它。我没有安装亚音速,所以它可能附带库。
答案 1 :(得分:1)
任何方式都不会在查询语法和LINQ的扩展方法语法中组合LINQ。使用下一个:
from t in db.All<DatabaseObject>()
where e.Category equals DataType
orderby t.DateOccurred descending
select t;
答案 2 :(得分:0)
这个问题与Subsonic解释某些linq语句的方式很明显,并且是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();
我已经通过声明IEnumerable类型的列表并使用ToList()来强制转换数据库交互来修复此问题,最后查询重新生成AsQueryable()