linq - How to add a extra value to the query in c# -
i have query this
var values = projectcontext.controloptions.where(i => i.id == id).select(i => new { i.value }).tolist();
now values contains 4 records want add 2 more records like
values.insert(0, new { "select" }); values.add("addnew");
but showing error. i.value
varchar
field in database.
how can add 2 records in list without field.
you selecting anonymous types. can add object of type matching names of properties , types, this:
var values = new[]{ new options{value = 666, id = 5}} //array demo purposes .where(i => i.id == 5) .select(i => new { value = i.value.tostring() }) .tolist(); values.add(new {value = "select"}); values.insert(0, new {value = "addnew"});
now values
contain 3 object of anonymous type (one property value
of type string
) , can printed as
addnew 666 select
consider creating new custom class , using in select
projection, this:
class mycustomclass { public string value { get; set; } }
and create object in select
list<mycustomclass> values = new[]{ new options{value = 666, id = 5}} .where(i => i.id == 5) .select(i => new mycustomclass { value = i.value.tostring() }) .tolist(); values.add(new mycustomclass { value = "select"}); values.insert(0, new mycustomclass { value = "addnew"});
now list of named type list<mycustomclass>
, elements can added , removed named classes
Comments
Post a Comment