0

Can someone help me to assign the value of 0 if no records found for this Linq query. Currently I am returning null, But i need to assign 0 if it is null.

//Linq Query
var ideasQuery = from b in db.StarLive_StarIBox_Ideas.Where(b => b.UserId == userid)
                         select new IdeasDTO()
                         {
                             IdeaId = b.IdeaId,
                             Idea = b.Idea,
                             UserName = b.StarLive_Sys_Users.cLogName,
                             DatePosted = b.DatePosted.ToString(),
                             Problem = b.StarLive_StarIBox_Problems.Problems,
                             Department = b.StarLive_Sys_Users.nDeptID.ToString(),
                             LikeId=b.StarLive_StarIBox_Likes.Where
                             (l=>l.IdeaId==b.IdeaId && l.LikedBy==b.UserId).FirstOrDefault().LikeID


                    };

//DTO Class
public class IdeasDTO
{
    [DataMember]
    public int IdeaId { get; set; }

    [DataMember]
    public string UserName { get; set; }

    [DataMember]
    public string Idea { get; set; }

    [DataMember]
    public string DatePosted { get; set; }

    [DataMember]
    public string Problem { get; set; }

    [DataMember]
    public int Likes { get; set; }

    [DataMember]
    public bool Liked { get; set; }

    [DataMember]
    public string Department { get; set; }

    [DataMember]
    public int? LikeId { get; set; }
}

//Postman Response

{
        "IdeaId": 21,
        "UserName": "Gowthamb",
        "Idea": "This is an Idea",
        "DatePosted": "Feb 14 2019  3:19PM",
        "Problem": "Problem 1",
        "Likes": 0,
        "Liked": false,
        "Department": "1",
        "LikeId": null
    }

Please help

EzLo
  • 13,780
  • 10
  • 33
  • 38
  • A possible duplication of https://stackoverflow.com/questions/12972295/firstordefault-default-value-other-than-null – Nanna Feb 21 '19 at 08:35

2 Answers2

1

Change this:

//Linq Query
var ideasQuery = from b in db.StarLive_StarIBox_Ideas.Where(b => b.UserId == userid)
                         select new IdeasDTO()
                         {
                             IdeaId = b.IdeaId,
                             Idea = b.Idea,
                             UserName = b.StarLive_Sys_Users.cLogName,
                             DatePosted = b.DatePosted.ToString(),
                             Problem = b.StarLive_StarIBox_Problems.Problems,
                             Department = b.StarLive_Sys_Users.nDeptID.ToString(),
                             LikeId=b.StarLive_StarIBox_Likes.Where
                             (l=>l.IdeaId==b.IdeaId && l.LikedBy==b.UserId).FirstOrDefault().LikeID


                    };

To this:

//Linq Query
var ideasQuery = from b in db.StarLive_StarIBox_Ideas.Where(b => b.UserId == userid)
                         select new IdeasDTO()
                         {
                             IdeaId = b.IdeaId,
                             Idea = b.Idea,
                             UserName = b.StarLive_Sys_Users.cLogName,
                             DatePosted = b.DatePosted.ToString(),
                             Problem = b.StarLive_StarIBox_Problems.Problems,
                             Department = b.StarLive_Sys_Users.nDeptID.ToString(),
                             LikeId=b.StarLive_StarIBox_Likes.Where
                             (l=>l.IdeaId==b.IdeaId && l.LikedBy==b.UserId).FirstOrDefault().LikeID ?? 0


                    };

Explanation:

LikeID is a int? it means that it is nullable.

the ?? operator, means that if the result is null it must assign the value on the right, which will be 0 in this case:

LikeId=b.StarLive_StarIBox_Likes.Where(l=>l.IdeaId==b.IdeaId && l.LikedBy==b.UserId).FirstOrDefault().LikeID ?? 0
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
0

You can write something like this change LikeId=b.StarLive_StarIBox_Likes to LikeId=b.StarLive_StarIBox_Likes == null ? 0 : b.StarLive_StarIBox_Likes

Bhargav Aboti
  • 278
  • 2
  • 11