2

我正在使用 .NET 3.5 在我的 DataLayer 类中,我引用了 System.Core、System.Data.Linq、System.Data.DataSetExtensions。但是如果我有Option Strict ON ,我不能在 Linq 查询中使用这个功能:

    Dim query = From st In db.Students _
         From c In db.Countries.Where(Function(c) c.Id = st.CountryId).DefaultIfEmpty _
         From r In db.Rooms.Where(Function(r) r.Id = st.RoomId).DefaultIfEmpty _
         From b In db.Buildings.Where(Function(b) b.Id = r.BuildingId).DefaultIfEmpty _
         From es In db.Essays.Where(Function(es) es.StudentId = st.Id).DefaultIfEmpty _
         Select st.Id, st.FullName, c.CountryName, r.RoomNumber, b.BuildingName, es.Eassay

它将产生以下错误:

Overload resolution failed because no accessible 'Where' can be called with these arguments:
    Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of Country, Integer, Boolean))) As System.Linq.IQueryable(Of Country)'

defined in 'System.Linq.Queryable': Nested function does not have the same signature as delegate 'System.Func(Of Country, Integer, Boolean)'.

Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of Country, Boolean))) As System.Linq.IQueryable(Of Country)' defined in 'System.Linq.Queryable': Option Strict On disallows implicit conversions from 'Boolean?' to 'Boolean'.
    Extension method 'Public Function Where(predicate As System.Func(Of Country, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of Country)' defined in 'System.Linq.Enumerable': Nested function does not have the same signature as delegate 'System.Func(Of Country, Integer, Boolean)'.
    Extension method 'Public Function Where(predicate As System.Func(Of Country, Boolean)) As System.Collections.Generic.IEnumerable(Of Country)' defined in 'System.Linq.Enumerable': Option Strict On disallows implicit conversions from 'Boolean?' to 'Boolean'..........

“Where”子句是 System.Linq.Queryable 的成员

公共共享函数 Where(Of TSource)(ByVal source As System.Linq.IQueryable(Of TSource), ByVal predicate As System.Linq.Expressions.Expression(Of System.Func(Of TSource, Boolean))) As System.Linq。 IQueryable(TSource)

而“DefaultIfEmpty”是 System.Linq.Queryable 的成员

公共共享函数 DefaultIfEmpty(Of TSource)(ByVal source As System.Linq.IQueryable(Of TSource)) As System.Linq.IQueryable(Of TSource)

如果我将Option Strict OFF设​​置为没有问题

如何在 Option Strict ON 的 VB.NET 项目中使用这些 System.Linq 扩展方法?谢谢

4

1 回答 1

5

看起来 CountryId 是一个可为空的值类型,因此

c.CountryId = st.CountryId

将是一个可为空的布尔值,而不是一个常规的布尔值。

尝试这样的事情

From st In db.Students _
From c In db.Countries.Where(Function(c) If(c.CountryId = st.CountryId, False)) _
Select st.FirstName, c.CountryName

顺便说一句,您似乎正在寻找一个团体加入:

From s In db.Students
Group Join c In db.Countries On s.CountryID Equals c.CountryID Into Group
From g In Group.DefaultIfEmpty
Select New With {.Name = s.Name, .CountryName = If(g IsNot Nothing, g.CountryName, "")}
于 2011-02-02T10:22:53.823 回答