hi experts
1. a sql server 2008 table that contains many columns and some of them are checked "not null"
like this:
create
table
Book(
ID
bigint
identity
(
1
,
1
)
not
null
,
Name
nvarchar
50
null
,
ISBN
nvarchar
10
not
null
,
......
2. in c#4, visual studio 2010, i use linq to sql. like:
var
context =
new
Model().Books;
var
query =
from
b
in
context
select
b;
the question is , how can i get only "allowed null" columns? i want linq to sql realize if the field is "allow null" or not?
This might help:
http://weblogs.asp.net/grantbarrington/archive/2009/03/17/using-reflection-to-determine-whether-an-type-is-nullable-and-get-the-underlying-type.aspx
[
^
]
There is some information on getting Nullable on msdn (
http://msdn.microsoft.com/en-us/library/ms366789.aspx
[
^
]), but only for types. You cannot use the
object.GetType()
to determine if something is Nullable because if you do a
GetType()
on an
int?
you just get the result of int. This code will work with
typeof()
(ie
typeof(int?)
):
public
static
class
Extensions
public
static
bool
IsNullable(
this
Type type)
return
type.IsGenericType
&& (type.GetGenericTypeDefinition() ==
typeof
(Nullable<>));
This code will work with generics;
public class x
<
T
>
public x()
var x = typeof(T).IsNullable();
please read this topic from stackoverflow, I think it's what you want to achieve.
http://stackoverflow.com/questions/413084/equivalent-of-sql-isnull-in-linq
[
^
]
Regards
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.