Reputation: 6781
I am trying to fill my ViewModel
from a few different LINQ queries but I am running into the problem of trying to fill multiple properties from a single LINQ query but I get the error
invalid anonymous type member declarator. anonymous type members must be declared with a member assignment, simple name or member access
I have done some searches and founds some posts but they are all about completely filling a ViewModel
and not just a few properties like I am trying to do. What am I supposed to do to fix this, or am I going about this completely wrong?
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();
}
My Model:
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; }
}
Upvotes: 0
Views: 426
Reputation: 14614
You can't assign a variable inside an anonymous type declaration. You need to select the anonymous type variable first and assign its properties to the model properties one by one. Change this part
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();
to this
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;
The third query below has the same problem
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();
I assume there might be more than one choices, so change your model as below
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; }
}
and change the third query to this
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));
}
Upvotes: 1