-1

Possible Duplicate:
Equivalent of SQL ISNULL in LINQ?
Using IsNull or select COALESCE in Linq..?

I've tried this query in LINQ:

   string query = @"SELECT ISNULL(P.firstname, s.firstname) AS Expr1,ISNULL(P.lastname,
  s.lastname) AS Expr2 FROM comment AS C LEFT OUTER JOIN professor AS P ON P.ID =   

  C.PID LEFT OUTER JOIN student AS s ON s.ID = C.SID

  WHERE (C.VID = 2)";

        ArrayList allNames=null;
        using (var context = new NewsReaderEntities())
        {
            ObjectQuery<string> results = context.CreateQuery<string>(query);
         //   ObjectQuery<string> results1 = context.CreateQuery<string>
             (query1,parameters);
            foreach (string result in results )
            {
                allNames.Add(result);

            }
        }

        return allNames;



    }

but it returns the error:

linq 'ISNULL' cannot be resolved into a valid type or function. Near simple identifier,

I've also tried this query:

SELECT COALESCE(p.firstname, s.firstname), COALESCE(p.lastname, s.lastname)
  FROM comment c
  LEFT JOIN Professor p
    ON c.pid = p.id
  LEFT JOIN Student s
    ON c.sid = s.id
 WHERE c.vid = 2

This also raises an error.

Both work okay in SQL Management. Any ideas?

Community
  • 1
  • 1
M.A.
  • 1
  • 3
  • 9
  • 1
    Have you tried a google search "linq isnull" - it returns this SO question which I believe will answer your question - http://stackoverflow.com/questions/413084/equivalent-of-sql-isnull-in-linq – Chris Gessler Jun 16 '12 at 13:26

1 Answers1

0

See this example:

var query = from p in Pets select p;
if (OwnerID != null) query = query.Where(x => x.OwnerID == OwnerID);
if (AnotherID != null) query = query.Where(x => x.AnotherID == AnotherID);
if (TypeID != null) query = query.Where(x => x.TypeID == TypeID);

Hope this help you

WizardDBA
  • 46
  • 4