0

本期与上期类似。我收到无效的匿名类型成员错误。然而,我已经修复它并确保它与我试图提取的给定数据相匹配。

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
                                });
4

1 回答 1

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
})...
于 2013-10-31T19:57:23.003 回答