【问题标题】:Invalid anonymous type error selecting mutiple columns in LINQ to Entities在 LINQ to Entities 中选择多个列时出现无效的匿名类型错误
【发布时间】:2014-09-28 12:30:01
【问题描述】:

我正在尝试从几个不同的 LINQ 查询中填充我的 ViewModel,但我遇到了尝试从单个 LINQ 查询中填充多个属性的问题,但我收到了错误

无效的匿名类型成员声明器。必须使用成员分配、简单名称或成员访问来声明匿名类型成员

我已经进行了一些搜索并找到了一些帖子,但它们都是关于完全填充 ViewModel 而不仅仅是像我试图做的一些属性。我应该怎么做才能解决这个问题,还是我完全错了?

using (ForumContext db = new ForumContext())
{
    model.ID = db.yaf_Topic
                .Where(t => t.ForumID == 5)
                .OrderByDescending(t => t.Posted)
                .Select(t => t.PollID.Value).First();

    model = db.yaf_Poll
                .Where(p => p.PollID == model.ID)
                .Select(p => new
                {
                    model.Question = p.Question,
                    model.IsMultipleChocie = p.AllowMultipleChoices,
                    model.ExperationDate = p.Closes
                })
                .First();

    model.Choices = db.yaf_Choice
                        .Where(c => c.PollID == model.ID)
                        .Select(c => new
                        {
                            model.Votes.Key = c.Choice,
                            model.Votes.Value = c.Votes,
                        })
                        .ToList();

    model.VotedIPs = db.yaf_PollVote
                        .Where(p => p.PollID == model.ID)
                        .Select(p => p.RemoteIP)
                        .ToList();

    model.VotedUserIDs = db.yaf_PollVote
                            .Where(p => p.PollID == model.ID)
                            .Select(p => p.UserID)
                            .ToList();
}

我的模特:

public class PollVM
{
    public int ID { get; set; }
    public string Question { get; set; }
    public bool IsMultipleChocie { get; set; }
    public DateTime? ExperationDate { get; set; }
    public KeyValuePair<string, int> Choices { get; set; }
    public List<string> VotedIPs { get; set; }
    public List<int?> VotedUserIDs { get; set; }
}

【问题讨论】:

  • 执行第二个查询时是否出现错误?

标签: c# asp.net-mvc linq-to-entities


【解决方案1】:

您不能在匿名类型声明中分配变量。您需要先选择匿名类型变量,并将其属性一一赋值给模型属性。改变这部分

model = db.yaf_Poll
            .Where(p => p.PollID == model.ID)
            .Select(p => new
            {
                model.Question = p.Question,
                model.IsMultipleChocie = p.AllowMultipleChoices,
                model.ExperationDate = p.Closes
            })
            .First();

到这里

var poll = db.yaf_Poll
            .Where(p => p.PollID == model.ID)
            .Select(p => new
            {
                p.Question,
                p.AllowMultipleChoices,
                p.Closes
            })
            .First();

model.Question = poll.Question;
model.IsMultipleChocie = poll.AllowMultipleChoices;
model.ExperationDate = poll.Closes;

下面的第三个查询也有同样的问题

model.Choices = db.yaf_Choice
                    .Where(c => c.PollID == model.ID)
                    .Select(c => new
                    {
                        model.Votes.Key = c.Choice,
                        model.Votes.Value = c.Votes,
                    })
                    .ToList();

我认为可能有不止一种选择,所以如下更改您的模型

public class PollVM
{
    public PollVM()
    {
        this.Choices = new List<KeyValuePair<string, int>>();
    }

    public int ID { get; set; }
    public string Question { get; set; }
    public bool IsMultipleChocie { get; set; }
    public DateTime? ExperationDate { get; set; }
    public List<KeyValuePair<string, int>> Choices { get; set; }
    public List<string> VotedIPs { get; set; }
    public List<int?> VotedUserIDs { get; set; }
}

并将第三个查询更改为此

var choices = db.yaf_Choice
                    .Where(c => c.PollID == model.ID)
                    .Select(c => new
                    {
                        c.Choice,
                        c.Votes,
                    })
                    .ToList();

foreach (var ch in choices)
{
    model.Choices.Add(new KeyValuePair<string, int>(ch.Choice, ch.Votes));
}

【讨论】:

    猜你喜欢
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    相关资源
    最近更新 更多