2010-11-08 63 views
0

OK我有一個返回列表的基本查詢(的ObjectQuery,居然但我不認爲它很重要)LINQ的繼承和亞型(想篩選出基本類型)

我有一個基於約5亞型主要類型和我需要過濾它們。一切工作正常,直到我想要 結果是主要類型(不是子類)

基本上我有一個包含所有內容的查詢,然後我篩選出未在checkedbox列表中檢查的類型。 (asp.net)

可以說我有一個名爲任務的實體,並SubTask1和SubTask2從任務

 Dim AllTasks As Objects.ObjectQuery(Of Demo.Task) = dbContext.CompletedTasks(ctx, anyID) 'calling a compiled query but it doesnt matter. 
    'It could be Dim AllTasks = from t in ctx.Tasks select t 

    For Each itm As ListItem In chkTaskTypeList.Items 
     If Not itm.Selected Then 
      Select Case itm.Value 
       Case "SubTask1" 
        'this works as expected, and eliminates tasks that are of the subtype Subtask1 
        AllTasks = From ti In AllTasks Where Not TypeOf ti Is SubTask1 

       Case "SubTask2" 
        'This also works as I want by filtering out the various sub types 
        AllTasks = From ti In AllTasks Where Not TypeOf ti Is SubTask2 AndAlso Not TypeOf ti Is SubTaskWhatever 

       Case "SubTask" 
        **'This works as expected, but not like I need. It removes all the types when what I really want is to remove** 
        **'The results that are of the base type but NOT the ones that are of a subtype** 
        AllTasks = From ti In AllTasks Where Not TypeOf ti Is SubTask 

      End Select 
     End If 
    Next 

    lvwHistory.DataSource = AllTasks 

繼承如果我有亞型的限定日期數我大概可以這樣說(不TypeOf運算TI是SubTask和TypeOf TI是SubTask1),也... ,但我希望有一個更好的方法(和一個不打破如果一個新的子類型的頁面)

回答

0

你應該比較,如果類型是相等的

OnlyBaseClasses = From ti In AllTasks Where ti.GetType() = GetType(TheBaseClass) 
+0

這不適用於L2E(嘗試它)。 – 2010-11-08 16:53:46

+0

@Craig,現在當你這麼說的時候,我不需要試着去看看哪些部分不能很好地翻譯。 – 2010-11-08 17:59:43

0

這是difficult, but possible。但是,你應該重新考慮你的設計。這樣做通常會違反Liskov替代原則。

+0

任何sugestions?我基本上被困在EF和OOP之間。什麼是界面的EF等價物? – 2010-11-08 17:03:28

+0

我不認爲你會「卡住」。你的問題在OOP條款中似乎是錯誤的,而不僅僅是EF條款。這就是爲什麼我提出了LSP。恕我直言,你真正的問題是你的課堂設計不符合你的需求。 – 2010-11-08 17:39:19

+0

我很欣賞你的意見,但它們很好地滿足了需求,現在需求已經改變,包括特別報告和過濾:p doh ..大聲笑,是的,我應該知道,但這是第一個大的LOB使用EF4的應用程序我已經完成了,而且僅僅爲了報告目的而更改模型讓我感到很痛苦。 – 2010-11-08 18:02:09