【问题标题】:How to use System.Linq extension method in VB.NET project with Option Strict ON如何在 Option Strict ON 的 VB.NET 项目中使用 System.Linq 扩展方法
【发布时间】:2011-02-02 09:56:54
【问题描述】:

我正在使用 .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(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 扩展方法?谢谢

【问题讨论】:

标签: vb.net linq option-strict


【解决方案1】:

看起来 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, "")}

【讨论】:

  • 请查看我的更新查询。我需要修改这些查询,以便它与 Option Strict ON 一起使用。如果我使用这个“在 st.CountryID 等于 c.CountryID 的国家中加入 c”,我只会得到那些具有 CountryId 的记录。但我想让所有学生都知道这就是为什么我开始使用 db.Countries.Where(Function(c) c.Id = st.CountryId).DefaultIfEmpty 但它不适用于 Option Strick ON。如果您想查看数据库结构,请到这里stackoverflow.com/questions/4870964
  • 感谢 DB 结构,它让事情变得更清晰。正如我所说,您的问题是学生中的 CountryID 可以为空。我会更新我的答案...
  • 如果查询有任何选项并产生相同的结果,我也想尝试一下。谢谢。
  • 您可以更改您的 where 子句以使用 'If(c.CountryId = st.CountryId, False)' 而不是 'c.CountryId = st.CountryId',因为前者会产生 False如果 st.CountryId 什么都不是,则转到 where 子句。
  • 我尝试了您的第一个建议,但它仅在选择学生、国家和房间时才有效。当我添加另一行来选择像这样的论文表 From es In db.Essays.Where(Function(es) If(es.StudentId = st.Id, False)).DefaultIfEmpty _ 它会抛出相同的错误。有什么理由会这样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
相关资源
最近更新 更多