LINQ-实体查询功能出错 [英] Error in linq-entity query function

查看:71
本文介绍了LINQ-实体查询功能出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.  public BoardPost GetPostByID(Int64 PostID)
    {
2.           BoardPost result = null;
3.           using(FisharooDataContext dc = _conn.GetContext())
            {
4.               BoardPost post = dc.BoardPosts.Include("Account").Where(p =>  
                     p.PostID == PostID).FirstOrDefault();
5.               result = post;
6.               result.Account = post.Account;
            }
7.           return result;
    }



我在第6行出现错误,可能是由于第4行的编码错误或缺少了一些参考.请帮帮我.谢谢亲爱的朋友.

[edit]添加了pre标签,并且未选中将我的内容作为纯文本而不是HTML对待 [/edit]



I get error at line no 6 probably due to wrong coding at line no 4 or due to some missing references. Please help me out. Thank you dear friends.

[edit]Added pre tag and unchecked Treat my content as plain text, not as HTML[/edit]

推荐答案

您的BoardPost post = dc.BoardPosts...行返回null,因此您的result.Account = post.Account失败,发布为null.

每当您执行一些操作以取回对象时,请始终确保该对象存在,然后再尝试使用它.
It is clear that your BoardPost post = dc.BoardPosts... line is returning null so your result.Account = post.Account is failing with post being null.

Whenever you do something to get an object back, always ensure that the object exists before trying to use it.
if( post != null )
   result.Account = post.Account;



至于linq的语法,我没有发现任何错误.



As to the syntax of the linq, I don''t see anything wrong with it off the top of my head.


这篇关于LINQ-实体查询功能出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆