开发者

LINQ2SQL detecting null returned null value and convert it

开发者 https://www.devze.com 2023-03-20 10:50 出处:网络
I want to detect NULL value returned from LINQ2SQL query and convert it to something for example if :

I want to detect NULL value returned from LINQ2SQL query and convert it to something for example if :

public static String[] getAllStudents(string n)
{
var ret = from p in db.students

select p.st_fname + " " + p.st_mname +" " +p.st_lname ;

string[] st = ret.ToArray<String>();
return st;
}

if(p.st_lname== NULL ) from The database I want to cast it into something like "This is empty" so if I have in my table :

-------------------------------------------------
st_fname|| st_maname || st_lname 
-------------------------------------------------
x       || y         || NULL
--------------------开发者_如何学Go-----------------------------

I want to apply what I explained above on it .


In that case:

select (p.st_fname ?? "This is empty") + " " + (p.st_mname ?? "This is empty") + 
        " " + (p.st_lname ?? "This is empty");

Or if it's just the last name that can be null:

select p.st_fname + " " + p.st_mname + 
        " " + (p.st_lname ?? "This is empty");

The ?? operator (the null-coalescing operator) means take the value on the left-hand-side, unless it's null, in which case take the right-hand-side.


try write something like:

var ret = from p in db.students
select string.Format("{0} {1} {2}",p.st_fname,p.st_mname, p.st_lname==null?"Undefined":p.st_lname);
0

精彩评论

暂无评论...
验证码 换一张
取 消