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

Nar*_*ana 2 linq vb.net option-strict

我正在使用.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
Run Code Online (Sandbox Code Playgroud)

它将产生以下错误:

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'..........
Run Code Online (Sandbox Code Playgroud)

"Where"子句是System.Linq.Queryable的成员

公共共享函数Where(Of TSource)(ByVal source As System.Linq.IQueryable(Of TSource),ByVal谓词As System.Linq.Expressions.Expression(Of System.Func(Of TSource,Boolean)))As System.Linq. IQueryable(Of 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扩展方法?谢谢

slo*_*oth 5

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

c.CountryId = st.CountryId
Run Code Online (Sandbox Code Playgroud)

将是一个可以为空的布尔值而不是常规布尔值.

尝试这样的事情

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

顺便说一下,无论如何,你似乎正在寻找一个群体加入:

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, "")}
Run Code Online (Sandbox Code Playgroud)