-1

可能重复:
LINQ 中的 SQL ISNULL 等价物?
在 Linq 中使用 IsNull 或选择 COALESCE ..?

我在 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;



    }

但它返回错误:

linq 'ISNULL' 无法解析为有效的类型或函数。接近简单的标识符,

我也试过这个查询:

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

这也会引发错误。

两者在 SQL 管理中都可以正常工作。有任何想法吗?

4

1 回答 1

0

看这个例子:

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

希望这对你有帮助

于 2012-06-16T14:41:31.510 回答