here is my code:
firstAnswer = p.Answers.Select(z => z.vountcount, z.isSelected).FirstOrDefault()
In the select statement, it returns a syntax error. I a开发者_高级运维m trying to get more then one column.
var firstAnswer = p.Answers.FirstOrDefault().Select(new { VountCount = z.vountcount, IsSelected = z.isSelected });
You must specify a type. Var is the keyword allowing you to instantiate an anonymous type.
You'll either have to create a type or use anonymous types to capture that result:
Anonymous types:
var firstAnswer = p.Answers.Select(z => new { vountcount = z.vountcount, isSelected = z.isSelected }).FirstOrDefault();
The explicit naming is in most cases optional. Using the var keyword here ensures, that you can assign that anonymous result. If you want to hand that result to some method, it'll get difficult with anonymous types.
Dedicated type:
public class ReducedAnswer
{
public int vountcount { get; set; }
public bool isSelected { get; set; }
public ReducedAnswer()
{
}
}
ReducedAnswer firstAnswer = p.Answers.Select(z => new ReducedAnswer { vountcount = z.vountcount, isSelected = z.isSelected }).FirstOrDefault();
Kept it close to typical LINQ model classes and your naming. Note the use of the type in front of firstAnswer
. You could go with var
here as well.
精彩评论