Linq FirstOrDefault in Group Join

时间:2017-10-04 15:36:29

标签: vb.net linq linq-to-sql

我正在Linq中处理一个基本上连接两个数据库和多个表的查询。第一个查询工作正常,然后创建为列表。然后我使用Linq将此列表加入第二个数据库,一旦我不尝试访问第二个数据库表中的任何字段,它就可以工作。

工作示例允许我在表达式中使用该组,但始终返回true。

工作:

Dim Transactions = (From ft In folioTransactions
                        Group Join t1 In (
                            From t In kdb.TaggedTransactions
                            Where t.FolioNumber = Me.FolioNumber
                            ) On ft.TrnNo Equals t1.TrnNo Into g = Group
                        Select New grid_Transaction With {
                            .TrnNo = ft.TrnNo,
                            .PostingDate = ft.PostingDate,
                            .Name = ft.Name,
                            .Reference = ft.Reference,
                            .Comment = ft.Comment,
                            .AmountPurMemo = ft.AmountPurMemo,
                            .SubFolioNo = ft.SubFolioNo,
                            .IsTagged = If(g IsNot Nothing, True, False)
                        }).ToList

一旦我尝试通过扩展该表达式来访问某个字段,它就会抛出一个Null Exception。

不工作:

Dim Transactions = (From ft In folioTransactions
                        Group Join t1 In (
                            From t In kdb.TaggedTransactions
                            Where t.FolioNumber = Me.FolioNumber
                            ) On ft.TrnNo Equals t1.TrnNo Into g = Group
                        Select New grid_Transaction With {
                            .TrnNo = ft.TrnNo,
                            .PostingDate = ft.PostingDate,
                            .Name = ft.Name,
                            .Reference = ft.Reference,
                            .Comment = ft.Comment,
                            .AmountPurMemo = ft.AmountPurMemo,
                            .SubFolioNo = ft.SubFolioNo,
                            .IsTagged = If(g IsNot Nothing, If(g.FirstOrDefault.KTBarcode IsNot Nothing, True, False), False)
                        }).ToList

并非所有字段都需要评估,我尝试通过简单地使用FirstOrDefault和SingleOrDefault进行访问,但仍然不允许我访问组字段而不抛出空例外

不工作:

Dim Transactions = (From ft In folioTransactions
                        Group Join t1 In (
                            From t In kdb.TaggedTransactions
                            Where t.FolioNumber = Me.FolioNumber
                            ) On ft.TrnNo Equals t1.TrnNo Into g = Group
                        Select New grid_Transaction With {
                            .TrnNo = ft.TrnNo,
                            .PostingDate = ft.PostingDate,
                            .Name = ft.Name,
                            .Reference = ft.Reference,
                            .Comment = ft.Comment,
                            .AmountPurMemo = ft.AmountPurMemo,
                            .SubFolioNo = ft.SubFolioNo,
                            .KTBarcode = If(g IsNot Nothing, g.FirstOrDefault.KTBarcode, Nothing)
                        }).ToList

如果有人能指出我正确的方向会很棒。我对VB很新,但不确定这是否特定于VB或者linq是如何工作的。

0 个答案:

没有答案