0

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
-------------------------------------------------

I want to apply what I explained above on it .

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.