2016-03-27 6 views
1
int userId; 
int follow = 0; 
if (Session["userId"] != null) 
{ 
    userId = int.Parse(Session["userId"].ToString()); 
    follow = Model.Follow.Count(x => x.EdenUserId == userId); 
} 

エラーコード:値をNullにすることはできません。Database.Count NULL値

誰かに従わないユーザーもいるので、Countは0にする必要がありますが、nullを返します。

ユーザーが任意のユーザーをフォローしている場合は、問題なくテーブルに入力されます。

ラムダ式がnullを返す可能性がある場合は、どのように数えることができますか?

答えて

0

試してみてください。

int userId; 
int follow = 0; 
if (Session["userId"] != null) 
{ 
    userId = int.Parse(Session["userId"].ToString()); 
    follow = Model.Follow.Count(x => x.EdenUserId == userId) == null? 0: Model.Follow.Count(x => x.EdenUserId == userId); 
}