code_junkie

Equivalent of SQL ISNULL in LINQ?

Nov 13th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. var hht = from x in db.HandheldAssets
  2. join a in db.HandheldDevInfos on x.AssetID equals a.DevName into DevInfo
  3. from aa in DevInfo.DefaultIfEmpty()
  4. select new
  5. {
  6. AssetID = x.AssetID,
  7. Status = xx.Online
  8. };
  9.  
  10. select new {
  11. AssetID = x.AssetID,
  12. Status = aa == null ? (bool?)null : aa.Online; // a Nullable<bool>
  13. }
  14.  
  15. select new {
  16. AssetID = x.AssetID,
  17. Status = aa == null ? false : aa.Online;
  18. }
  19.  
  20. using(var ctx = new DataClasses1DataContext())
  21. {
  22. ctx.Log = Console.Out;
  23. var qry = from boss in ctx.Employees
  24. join grunt in ctx.Employees
  25. on boss.EmployeeID equals grunt.ReportsTo into tree
  26. from tmp in tree.DefaultIfEmpty()
  27. select new
  28. {
  29. ID = boss.EmployeeID,
  30. Name = tmp == null ? "" : tmp.FirstName
  31. };
  32. foreach(var row in qry)
  33. {
  34. Console.WriteLine("{0}: {1}", row.ID, row.Name);
  35. }
  36. }
  37.  
  38. SELECT [t0].[EmployeeID] AS [ID],
  39. (CASE
  40. WHEN [t2].[test] IS NULL THEN CONVERT(NVarChar(10),@p0)
  41. ELSE [t2].[FirstName]
  42. END) AS [Name]
  43. FROM [dbo].[Employees] AS [t0]
  44. LEFT OUTER JOIN (
  45. SELECT 1 AS [test], [t1].[FirstName], [t1].[ReportsTo]
  46. FROM [dbo].[Employees] AS [t1]
  47. ) AS [t2] ON ([t0].[EmployeeID]) = [t2].[ReportsTo]
  48. -- @p0: Input NVarChar (Size = 0; Prec = 0; Scale = 0) []
  49. -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
  50.  
  51. var hht = from x in db.HandheldAssets
  52. join a in db.HandheldDevInfos on x.AssetID equals a.DevName into DevInfo
  53. from aa in DevInfo.DefaultIfEmpty()
  54. select new
  55. {
  56. AssetID = x.AssetID,
  57. Status = xx.Online ?? false
  58. };
  59.  
  60. MySum = MyTable.Where(x => x.SomeCondtion).Sum(x => x.AnIntegerValue);
  61.  
  62. MySum = MyTable.Where(x => x.SomeCondtion).Sum(x => (int?) x.AnIntegerValue);
Add Comment
Please, Sign In to add comment