2

.NET 3.5を使用しています。DataLayerクラスには、System.Core、System.Data.Linq、System.Data.DataSetExtensionsの参照があります。ただし、 Option Strictをオンにしている場合、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のメンバーです

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)))AsSystem.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拡張メソッドを使用するにはどうすればよいですか?ありがとうございました

4

1 に答える 1

5

CountryIdはnull許容値型であるように見えるため、

c.CountryId = st.CountryId

通常のブール値ではなく、null許容のブール値になります。

このようなものを試してください

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, "")}
于 2011-02-02T10:22:53.823 に答える