2011-02-02 62 views
2

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

+0

如果你真的很無聊採用嚴格的,試用選項推斷在使類型推斷。只是一個想法,如果它違反規則/模式,然後忽略此:) http://msdn.microsoft.com/en-us/library/bb384665.aspx – Tom 2011-02-02 11:23:03

回答

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, "")} 
+0

請參閱我的更新查詢。我需要修改這些查詢,以便與Option Strict ON配合使用。如果我使用了「在國家代碼中加入c關於st.CountryID Equals c.CountryID」,我只能得到那些有CountryId的記錄。但我想讓所有學生都是這樣,爲什麼我要使用db.Countries.Where(Function(c)c.Id = st.CountryId).DefaultIfEmpty,但它不適用於Option Strick ON。如果你想看到數據庫結構,請到這裏http://stackoverflow.com/questions/4870964 – Narazana 2011-02-02 10:39:13