代码之家  ›  专栏  ›  技术社区  ›  stack247

ASP.NET Web API OData可以返回非集合财产吗?

  •  0
  • stack247  · 技术社区  · 11 年前

    我有以下课程:

        public class WeeklyReport
        {
            public IEnumerable<DailyReport> DailyReports { get; set; }
    
            public int? TotalReport
            {
                get
                {
                    return DailyReports.Sum(x => x.ReportId);
                }
            }
    
            public string Title
            {
                get
                {
                    return "Weekly Report";
                }
            }
        }
    
        public class DailyReport
        {
            public string Office { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }
            public string ReportTo { get; set; }
            public DateTime Collection { get; set; }
            public int? Leads { get; set; }
        }
    

    在OData控制器中的一个方法中,我返回WeeklyReport的IQueryable。 然而,当查询OData端点时,返回的JSON类似于:

    {
    "odata.metadata": "http://localhost:546/odata/$metadata#WeeklyReports",
    "value": [
        {
            "DailyReports": [
                {
                    "Office": "002",
                    "Name": "First Last",
                    "Type": 10,
                    "ReportTo": "00002",
                    "Collection": "2014-03-18T00:00:00",
                    "Leads": null
                },
                {
                    "Office": "002",
                    "Name": "Agent, ",
                    "Type": 10,
                    "ReportTo": "00002",
                    "Collection": "2014-03-18T00:00:00",
                    "Leads": null
                }
            ]
        },
        {
            "DailyReports": [
                {
                    "Office": "002",
                    "Name": "First Last",
                    "Type": 10,
                    "ReportTo": "00002",
                    "Collection": "2014-03-18T00:00:00",
                    "Leads": null
                },
                {
                    "Office": "002",
                    "Name": "Agent, ",
                    "Type": 10,
                    "ReportTo": "00002",
                    "Collection": "2014-03-18T00:00:00",
                    "Leads": null
                }
            ]
        }
    ]
    

    是否有方法将ASP.NET Web API OData设置为同时返回WeeklyReport.TotalReport和WeeklyReport.Title?

    谢谢

    1 回复  |  直到 11 年前
        1
  •  1
  •   Tan Jinfu    11 年前

    我转载了这个问题。我相信您正在使用ODataConventionModelBuilder来构建Edm模型。默认情况下,生成器仅添加带有public get的财产,并将其设置为Edm模型。在我添加了一个无运算集之后,问题就解决了。你可以试一试。

    public class WeeklyReport
    {
        public IEnumerable<DailyReport> DailyReports { get; set; }
    
        public int? TotalReport
        {
            get
            {
                return DailyReports.Sum(x => x.ReportId);
            }
            set{}
        }
    
        public string Title
        {
            get
            {
                return "Weekly Report";
            }
            set{}
        }
    }