Consider this simplified example query:
dt = (
from O in ...
...
select new
{
O.A,
B = 0
}).ToList().ToDataTable()
Where .ToDataTable()
comes from Convert generic List/Enumerable to DataTable?
Assuming that table O
does not have an attribute called B
but I want to have a column called B
whose values I will fill in later. This works fine when B
get number values sent to it but i actually want to send it int?
values and at present it crashes if it gets send a null
.
Is there a way to select a new column (eg B
) such that it is a nullable type?
You can do like this,
var testNull = (from student in studentList
select new
{
RollNo = default(int?),
Name = student.Name
}).ToList();
But better we can create concrete type instead of depending upon a anonymous type based on value.:-)
Since the type of anonymous type members is inferred make it infer the right thing: B = (int?)null
.
Not sure what you mean by "fill in later". If you want B
to be mutable you need to create a class.
Try:
select new
{
O.A,
B = (int?)0
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.