我正在使用.NET 3.5在我的DataLayer类中我引用了System.Core,System.Data.Linq,System.Data.DataSetExtensions 。如何在VB.NET项目中使用System.Linq扩展方法,并使用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
这将产生以下错误:但 如果我有选项严格ON我cantnot使用Linq查询此功能
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'..........
“去哪儿”的条款是系统的成员。 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)
而 “DefaultIfEmpty” 是System.Linq.Queryable
会员Public Shared Function DefaultIfEmpty(Of TSource)(ByVal source As System.Linq.IQueryable(Of TSource)) As System.Linq.IQueryable(Of TSource)
如果我设置选项严格关,没有任何问题。
如何在使用Option Strict ON的VB.NET项目中使用这些System.Linq扩展方法?谢谢
如果你真的很无聊采用严格的,试用选项推断在使类型推断。只是一个想法,如果它违反规则/模式,然后忽略此:) http://msdn.microsoft.com/en-us/library/bb384665.aspx – Tom 2011-02-02 11:23:03