I am facing this situation and it is really strange to me.
I have class A has the following
public class A
{
public A()
{
Values = new List<B>();
}
public virtual IList<B> Values { get; set; }
}
I have a method that will read all A records form the db.
var all = await GetAll();
here is my method
public async Task<IList<A>> GetAll(int id1)
{
var parameters = new[]
{
new SqlParameter("@ProductTypeId", SqlDbType.Int) {Value = id1},
};
return (await _sqlCommandWrapper.ExecuteReaderAsync("SPName", r => new A
{
values = JsonConvert.DeserializeObject<IList<B>>(r["Values"].ToString())
}, parameters)).ToList();
}
my db column r["Values"] will return json that can be deserialized to list of B class.
Then i am using linq to query all my A:
var subitems = all .Where(a=> a.Values .Count > 0).ToList();
If my r["Values"] return data, it works fine.
however if it is not then a.Values is null ?
Any reason, however I defined in my A constructor that Values = new List<B>();