在lambda表达式中做了什么新的{}?

时间:2015-03-11 05:58:29

标签: c#

我正在查看代码并发现以下代码行我无法理解......

objDashBoardHierarchyInfo = SubscribeList.GroupBy(x = > new {
    ReportID = x.ReportId, ReportName = x.ReportName, Direction = x.DisplayColumn
})
    .Select(x = > new DashBoardHierarchyInfo {
    ReportID = x.Key.ReportID,
    ReportName = x.Key.ReportName,
    DisplayDirection = x.Key.Direction ? ? 0,
    oListDashBoardReportInfo = x.Select(c = > new DashBoardReportInfo {
        DataSourceObjectName = c.DataSourceObjectName,
        ReportSubType = c.OutputType,
        DataSourceId = c.DataSourceId,
        XvalueFeild = "",
        YvalueFeild = "",
        ReportName = c.ReportName,
        BodyId = c.BodyID,
        ReportID = c.ReportId,
        UserDashboardReportId = c.UserDashboardReportId,
        ChartTypeID = c.ChartTypeID,
        SeriesColor = c.Color,
        SeriesControlTypeId = c.ControlType ? ? 0
    }).ToList < DashBoardReportInfo > ()
}).ToList < DashBoardHierarchyInfo > ();

2 个答案:

答案 0 :(得分:1)

new { }为您创建一个匿名类型,并创建返回的匿名类型的对象。您可以在此MSDN article上阅读有关匿名类型的更多信息。

您要求使用SubscribeList对象传递lambda来创建匿名类型对象的语句。

new { ReportID = x.ReportId, ReportName = x.ReportName, Direction = x.DisplayColumn }

您可以创建没有lamda的匿名类型的对象,如下所示。

var myObj = new {Id=1, Name="abc"};
Console.WriteLine(myObj.Id);
Console.WriteLine(myObj.Name);

答案 1 :(得分:1)

new { }语法创建anonymous type的实例。

new { ReportID = x.ReportId, ReportName = x.ReportName, Direction = x.DisplayColumn }

表示我希望按照三个属性ReportIDReportNameDisplayColumn的每种可能组合对此列表进行分组。