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

发布于 2024-10-15 21:50:33 字数 2614 浏览 6 评论 0原文

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

I'm using .NET 3.5 In my DataLayer class I have references of System.Core,System.Data.Linq, System.Data.DataSetExtensions. But
I cantnot use this feature in Linq query if I have Option Strict ON:

    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

It will yield the following error:

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'..........

The "Where" clause is Member of System.Linq.Queryable

Public Shared Function 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)

And "DefaultIfEmpty" is Member of System.Linq.Queryable

Public Shared Function DefaultIfEmpty(Of TSource)(ByVal source As System.Linq.IQueryable(Of TSource)) As System.Linq.IQueryable(Of TSource)

If I set Option Strict OFF, there's no problem.

How to use these System.Linq extension methods in VB.NET project with Option Strict ON ? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法言说的痛 2024-10-22 21:50:34

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

Looks like CountryId is a nullable value type, hence

c.CountryId = st.CountryId

will be a nullable boolean instead of a regular boolean.

Try something like this

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

BTW, it's seems your looking for a group join anyhow:

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, "")}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文