我有这个LINQ查询返回非静态方法异常,因为左连接有时会为context.Betas返回一个空值。Linq:Non-static-methods-requires-a-target with select new {model}
return (from t in context.Alphas
join b in context.Betas on new { Id = t.Id } equals new { Id = b.AlphaId } into b_leftjoin
from b in b_leftjoin.DefaultIfEmpty()
where
t.UserProfileId == userProfileId
&& t.IsClosed == false
&& t.IsCancel == false
&& t.EndDate <= DateTime.Now
orderby
t.Title
select new AlphaSelection()
{
Title = t.Title,
CurrentUser = b.UserProfile == null ? null : b.UserProfile,
BetaId = b.Id == null ? 0 : b.Id,
ProjectNumber = t.ProjectNumber,
AlphaId = t.Id
}).ToList();
如果我删除CurrentUser和BetaId查询工作,但我需要保留所有信息在一起。你能帮我解决这个问题吗?
谢谢!
编辑(回答到评论):
实际的例外是这个:
非静态方法需要一个目标。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。
Exception Details: System.Reflection.TargetException: Non-static method requires a target.
Source Error:
Line 39: else
Line 40: {
Line 41: return query.ToList();
Line 42: }
Line 43: }
什么是**实际**异常? –
你能给出例外的*确切的*细节吗?目前还不清楚。 –
'Id'的类型是什么?你为什么使用'new {}'来进行连接? – Guvante