无效的匿名类型成员声明 [英] Invalid anonymous type member declarator

查看:321
本文介绍了无效的匿名类型成员声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码应该工作的一个问题,根据的这个MSDN论坛帖子

I have a problem with the following code which should work, according to this MSDN Forums post.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LINQTest
{
    class Program
    {
        class Schedule
        {
            public int empid { get; set; }
            public int hours { get; set; }
            public DateTime date { get; set; }
            public DateTime weekending { get; set; }
        }

        static void Main(string[] args)
        {
            List<Schedule> Schedules = new List<Schedule>();

            var bla = from s in Schedules
                      group s by new { s.empid, s.weekending} into g
                      select new { g.Key.empid, g.Key.weekending, g.Sum(s=>s.hours)};
        }
    }
}



我收到错误用SUM函数:
无效的匿名类型成员声明。匿名类型成员必须与成员分配,简单名称或成员访问来声明。

I'm getting the error with the sum function: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

怎么了?

推荐答案

您必须命名用于存储总和方法结果的属性:

You have to name the property used to store Sum method result:

select new { g.Key.empid, g.Key.weekending, Sum = g.Sum(s=>s.hours)};



编译器不能推断,当你从指定的表达式值的属性名称:

Compiler can't infer the property name when you're assigning the value from expression:

匿名类型(C#编程指南)

您必须为正与一个表达式(...)

You must provide a name for a property that is being initialized with an expression (...)

这篇关于无效的匿名类型成员声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆