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
Linq 林克集团按月_Linq_Subsonic_Group By - Fatal编程技术网

Linq 林克集团按月

Linq 林克集团按月,linq,subsonic,group-by,Linq,Subsonic,Group By,我在获取按月份和年份分组的(亚音速)对象的可查询列表时遇到问题 对象的基本视图 public partial class DatabaseObject { [SubSonicPrimaryKey] public int objectID { get; set; } public string Description { get; set; } public decimal Value { get; set; } public string Categ

我在获取按月份和年份分组的(亚音速)对象的可查询列表时遇到问题

对象的基本视图

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(字符串数据类型)
{
返回值(从t开始,单位为db.All()
orderby t.DATE降序发生
选择t)
其中(e=>e.Category==数据类型);
}
我的问题是,如何返回按月份分组的日期?我尝试了以下方法,但这会导致编译器发出有关匿名类型的警告

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;
}
public IQueryable GetData(字符串数据类型)
{
var datalist=(从db.All()中的t开始,其中(e=>e.Category==DataType);
设m=new
{
月=t.dateoccurrent.month,
年份=t.date.year
}
将t按m分组到l选择新
{
Description=string.Format(“{0}/{1}”,l.Key.month,l.Key.year),
Value=l.Sum(v=>v.Value),//Sum(v=>v.Value),
Category=“分组”
dateoccurrent=l.Last(v=>v.dateoccurrent)
}
返回数据列表;
}

有什么想法吗?

试试我发现的这两个问题,但基本上需要选择数据库对象而不是匿名类型

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();
IQueryable数据列表=(
从db.All()中的t开始,其中(e=>e.Category==DataType)
设m=new
{
月=t.dateoccurrent.month,
年份=t.date.year
}
将t按m分组为l
选择新数据库对象()
{
Description=string.Format(“{0}/{1}”,l.Key.month,l.Key.year),
Value=l.Sum(v=>v.Value),//Sum(v=>v.Value),
Category=“Grouped”,
dateoccurrent=l.Max(v=>v.dateoccurrent)
}).AsQueryable();

让我知道我的解决方案是否符合您的要求。我还注意到您使用的是最后一个?您使用的扩展我没有,因此我将其替换为Max。我没有安装亚音速,因此它可能随库一起提供。

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

from t in db.All<DatabaseObject>()    
where e.Category equals DataType
orderby t.DateOccurred descending
select t;
来自t,单位为db.All()
其中e.Category等于DataType
orderby t.DATE降序发生
选择t;

这个问题显然与亚音速对话某些linq语句的方式有关,并且是一个问题

IEnumerable数据列表=(
从db.All()中的t开始,其中(e=>e.Category==DataType).ToList()
设m=new
{
月=t.dateoccurrent.month,
年份=t.date.year
}
将t按m分组为l
选择新数据库对象()
{
Description=string.Format(“{0}/{1}”,l.Key.month,l.Key.year),
Value=l.Sum(v=>v.Value),//Sum(v=>v.Value),
Category=“Grouped”,
dateoccurrent=l.Max(v=>v.dateoccurrent)
}).AsQueryable();

我已通过声明IEnumerable类型的列表并使用ToList()强制转换数据库交互来修复此问题,最后将查询重新转换为AsQueryable()

唯一的问题是,在尝试操作值时,我似乎做错了什么,描述为“01/01/1900 00:00:00”类别为空。我甚至尝试强制转换为自定义类型的SimpleObject,但问题仍然存在?我只是匹配了您的逻辑并修复了您的匿名类型问题…我给您的建议是解释您试图用您提供的查询完成的任务。或者将其发布到此处,或者作为新问题发布。可能有更好的方法来解决此问题完成你需要的。尼克斯,请看下面我的答案。匿名类型是一个很大的帮助,问题的另一部分是由于亚音速。我向任何希望调试其LINQ查询的人推荐LINQPad。有趣的时刻……你是否尝试过放置ToList()最后?删除ASQueryable?我希望它是可查询的,因为我使用的一些分页功能很适合这种类型。
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();