无效的匿名类型成员错误

时间:2013-10-31 19:56:37

标签: c#

此问题与上一期有关。我收到一个无效的匿名类型成员错误。然而,我已经完成了修复并确保它与我试图提取的给定数据相匹配。

var branches = ctx.tblCodeBrokBranches.Where(comp => comp.BrokOffID == 1);
                var activeBrokerLocs = branches.Select(b => new
                {
                    b.BrokOffID.ToString(), //Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
                    b.BrokBranch
                })
                                .AsEnumerable()
                                .Select(b => new SelectListItem
                                {
                                    Value = b.BrokOffID.ToString(),
                                    Text = b.BrokBranch
                                });

1 个答案:

答案 0 :(得分:5)

除非可以从属性或字段表达式推断出成员名称,否则您必须为该成员提供一个明确的名称:

var activeBrokerLocs = branches.Select(b => new
{
    BrokOffID = b.BrokOffID.ToString(),
    b.BrokBranch
})...

但是根据您的评论,似乎BrokOffID已经是一个字符串。在这种情况下,您根本不需要调用ToString,您可以让C#推断成员名称:

var activeBrokerLocs = branches.Select(b => new
{
    b.BrokOffID,
    b.BrokBranch
})...