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 - Fatal编程技术网

C# LINQ:从每个组中选择最新的

C# LINQ:从每个组中选择最新的,c#,linq,C#,Linq,所以,我知道我很生疏,但我从没想过这会如此困难,尽管我花了几个小时尝试不同的解决方案。在删除一个特定的子句并将其绑定到网格之后,我尝试从每个组中选择最新的记录。基本上没有运气。。。这就是我结束的地方: var widgets = db.Updates .GroupBy(c => c.widgetType) .SelectMany(s => s) .Where(c => c.Sold.Equals(false

所以,我知道我很生疏,但我从没想过这会如此困难,尽管我花了几个小时尝试不同的解决方案。在删除一个特定的子句并将其绑定到网格之后,我尝试从每个组中选择最新的记录。基本上没有运气。。。这就是我结束的地方:

var widgets = db.Updates
            .GroupBy(c => c.widgetType)
            .SelectMany(s => s)
            .Where(c => c.Sold.Equals(false))
            .OrderByDescending(x => x.TimeStamp)
            .FirstOrDefault();
        var list = new List<Update>() {widgets};
            widgetsGrid.DataSource = list;            
            widgetsGrid.DataBind();
var widgets=db.Updates
.GroupBy(c=>c.widgetType)
.SelectMany(s=>s)
其中(c=>c.sell.Equals(false))
.OrderByDescending(x=>x.TimeStamp)
.FirstOrDefault();
var list=newlist(){widgets};
widgetsGrid.DataSource=list;
widgetsGrid.DataBind();
我将强制转换添加到列表中,因为我得到了一个数据类型错误,目前它只返回所有记录的最后一项,而不是每个组的最后一项

提前感谢您的帮助

var widgets=db.Updates
         var widgets = db.Updates
        .GroupBy(c => c.widgetType)
        .SelectMany(s => s)
        .Where(c => c.Sold.Equals(false))
        .OrderByDescending(x => x.TimeStamp)
        .Select(y=>y.First());

    var list = new List<Update>() {widgets};
        widgetsGrid.DataSource = list;            
        widgetsGrid.DataBind();
.GroupBy(c=>c.widgetType) .SelectMany(s=>s) 其中(c=>c.sell.Equals(false)) .OrderByDescending(x=>x.TimeStamp) .选择(y=>y.First()); var list=newlist(){widgets}; widgetsGrid.DataSource=list; widgetsGrid.DataBind();
OrderByDescending应该在每个组的级别上。您也不需要
选择many
,因为这会将组重新加入到一个平面列表中

var widgets = Updates
        .GroupBy(c => c.widgetType)
        .Where(c => c.Sold.Equals(false))
        .Select(x => x.OrderByDescending(y => y.TimeStamp).First());

看起来你需要做的关键事情是按顺序排列并从列表中选择第一项。下面是一个示例程序:

class Program
{
    static void Main(string[] args)
    {
        var foodOrders = new List<FoodOrder>
        {
            new FoodOrder { FoodName = "hotdog", OrderDate = new DateTime(2016, 7, 7) },
            new FoodOrder { FoodName = "hamburger", OrderDate = new DateTime(2016, 7, 6) },
            new FoodOrder { FoodName = "taco", OrderDate = new DateTime(2016, 7, 5) },
        };

        var mostRecentFoodOrder = foodOrders.OrderByDescending(f => f.OrderDate).First().FoodName;
        Console.WriteLine(mostRecentFoodOrder);

        //cmd
        //hotdog
        //Press any key to continue . . .
    }
}

class FoodOrder
{
    public string FoodName { get; set; }
    public DateTime OrderDate { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
var foodOrders=新列表
{
new FoodOrder{FoodName=“hotdog”,OrderDate=new DateTime(2016,7,7)},
new FoodOrder{FoodName=“hamburger”,OrderDate=new DateTime(2016,7,6)},
new FoodOrder{FoodName=“taco”,OrderDate=new DateTime(2016,7,5)},
};
var mostRecentFoodOrder=foodOrders.OrderByDescending(f=>f.OrderDate).First().FoodName;
控制台写入线(mostRecentFoodOrder);
//指令
//热狗
//按任意键继续。
}
}
食品商
{
公共字符串FoodName{get;set;}
public DateTime OrderDate{get;set;}
}

我在网上看到这个问题被问了一百次。。。这对我有用。感谢@Ahmad Ibrahim

非常感谢@Ahmad Ibrahim。。。但还是不行。当我按您的方式运行代码时,我再次得到一个数据类型异常,因此我添加了一个“ToList”,仍然只得到最后一条记录,而不是每个组的最后一条记录。现在我有了:var widgets=db.Updates.GroupBy(c=>c.widgetType).Where(c=>c.sell.Equals(false)).Select(x=>x.OrderByDescending(y=>y.TimeStamp).First();var list=newlist(){widgets}
var list=widgets.ToList()
应该可以正常工作。。不能使用
newlist{widgets}var widgets = db.Updates
          .Where(c => c.Sold.Equals(false))
          .GroupBy(c => c.widgetType)       
      .Select(x => x.OrderByDescending(y => y.TimeStamp).First()).ToList();
       widgetGrid.DataSource = widgets;
       widgetGrid.DataBind();