诠释?和int比较时LEFT OUTER Linq中的联接问题 [英] int? and int comparison when LEFT OUTER JOIN in Linq issue

查看:120
本文介绍了诠释?和int比较时LEFT OUTER Linq中的联接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个WinForms项目需要使用的LINQ到SQL 。我已经能够创建一个使用 SqlMetal 工具我的DataContext,并提出一些疑问。但现在我有我还没有能够解决问题。

I am working on a WinForms project which requires to use Linq-To-Sql. I have been able to create my DataContext using the SqlMetal tool, and make some queries. But right now I have a problem that I havent been able to solve.

我试图让 LEFT OUTER JOIN 如下:

MyDatabase db = new MyDatabase(...);

 var query = from p in db.ParentTable
             join t in db.ChildTable on new {A = p.child_ID, B = p.OtherID}
             equals new {A = t.ID, B = t.OtherID} into j1
             from c in j1.DefaultIfEmpty()
             select new
             {
                  ...
             };

在我写这篇文章的查询错误是在编译时所提出的加入字:

When I write this query an error is raised at compile-time in the join word:

联接子句中的类型表现之一是不正确。类型推断在调用失败群组加入

The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'

我知道这个错误是由之间的比较造成p .child_ID t.ID ,因为 p.child_ID 诠释? t.ID INT 。但是,如何解决这个问题?我怎么能执行 LEFT OUTER JOIN 无需此错误??

I know this error is caused by the comparison between p.child_ID and t.ID since p.child_ID is int? and t.ID is int. But, How can I solve this? How can I perform the LEFT OUTER JOIN without having this error??

p.child_ID INT?,因为这列被标记为 IS NULL SQL

p.child_ID is int? since this column is marked as IS NULL in SQL.

希望有人能帮助我,在此先感谢。

Hope someone can help me, thanks in advance.

推荐答案

您可以使用 GetValueOrDefault()方法。它获取当前可空对象或对象的默认值的值

You can use GetValueOrDefault() method. It retrieves the value of the current Nullable object, or the object's default value.

在你的例子来说:

var query = from p in db.ParentTable
            join t in db.ChildTable on new {A = p.child_ID.GetValueOrDefault(0), B = p.OtherID}
            equals new {A = t.ID, B = t.OtherID} into j1
            from c in j1.DefaultIfEmpty()
            select new
            {
                ...
            };

如果该p.child_ID成为空比将返回0。希望这将帮助!

If the p.child_ID become null than it will return 0. Hope this will help !!

这篇关于诠释?和int比较时LEFT OUTER Linq中的联接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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