本文介绍了查找平均值时linq查询出错无法将null值分配给类型为System.Decimal的成员,该成员是非可空值类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dl_books.DataSource = (from st in obj.Book_details
                                      join d in obj.Rating_tables on st.Book_Id equals d.Book_id into pl
                                      where st.Category == "Health "
                                      orderby st.Book_Id descending


                                      select new { st.Book_Id , rat = pl.Average(c=>Convert.ToDecimal(c.Rating))  ,st.Price ,st.Name ,st.Image, }).Take(6);
               dl_books.DataBind();




this is my code and below is my error may be it is just bcz 4 some books i dnt having rates so then database is producing null and dts d problem any idea how to resolve this 




The null value cannot be assigned to a member with type System.Decimal which is a non-nullable value type.

推荐答案

dl_books.DataSource = (from st in obj.Book_details
                                      join d in obj.Rating_tables on st.Book_Id equals d.Book_id into pl
                                      where st.Category == "Health "
                                      orderby st.Book_Id descending
 

                                      select new { st.Book_Id , rat = pl.Average(c=>(decimal)c.Rating)  ,st.Price ,st.Name ,st.Image, }).Take(6);
               dl_books.DataBind();


这篇关于查找平均值时linq查询出错无法将null值分配给类型为System.Decimal的成员,该成员是非可空值类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:06