2010-07-12 67 views
1
Public Function List_category() As Myobj 
Dim query = From subcat In _dataContext.subcategories, _ 
cat In _dataContext.categories _ 
Where subcat.CategoryID = cat.CategoryID _ 
       Select New Myobj() With { _ 
       .SubcatId = subcat.SubCategoryID, _ 
       .SubcatName = subcat.SubCategoryName, _ 
       .CatId = cat.CategoryID, _ 
       .CatName = cat.CategoryName _ 
       } 
return ????????? 
End Function 

公共類MyObj中我的對象查詢返回什麼類型?

Private m_SubcatId As Integer 
Private m_SubcatName As String 
Private m_catId As Integer 
Private m_catName As String 

Public Property SubcatId() As Integer 
    Get 
     Return m_SubcatId 
    End Get 
    Private Set(ByVal value As Integer) 
     m_SubcatId = value 
    End Set 
End Property 

Public Property SubcatName() As String 
    Get 
     Return m_SubcatName 
    End Get 
    Private Set(ByVal value As String) 
     m_SubcatName = value 
    End Set 
End Property 

Public Property CatId() As Integer 
    Get 
     Return m_catId 
    End Get 
    Private Set(ByVal value As Integer) 
     m_catId = value 
    End Set 
End Property 

Public Property CatName() As String 
    Get 
     Return m_catName 
    End Get 
    Private Set(ByVal value As String) 
     m_catName = value 
    End Set 
End Property 

末級

犯規作品!!!!

它說屬性'SubcatName'的'Set'訪問器不可訪問。

+0

易上問號,只是想冷靜,冷靜,理性的思考,我們將度過這次難關。 – 2010-07-12 12:01:39

回答

0

您可以創建自定義類型並修改您的select以實例化返回。看看這裏:Linq To Sql return from function as IQueryable<T>

+0

謝謝,但你可以在這裏舉一個例子 – 2010-07-12 12:55:03

+0

嗯,好的。我的VB可能在語法上略有差異,但這是一般想法(注意,您需要創建MyObject類,當然):從子類中返回在_dataContext.subcategories中,cat在_dataContext.categories中Where subcat.CategoryID = cat.CategoryID使用{.SubCategoryID = subcat.SubCategoryId,.SubCategoryName = subcat.SubCategoryName,.CategoryID = subcat.CategoryID,.CategoryName = cat.CategoryName}選擇新的MyObject() – David 2010-07-12 13:07:23

+0

Public Function List()As ????? ????????? 將是 公共函數列表()作爲MyObject 是嗎? – 2010-07-12 13:36:23

0

編譯器只是告訴你,你已經在SubcatName上聲明瞭私有集,但是ypou正試圖在New Myobj()之後爲它賦值。

因爲你可以聲明一個POD類(普通的舊數據 - 只是公共數據,沒有方法或屬性)第一次運行中,一旦你看到它runnung你可以去調整它,添加方法等

如果它是所有屬性都是隻讀的非常重要,您需要嘗試使查詢方法成爲同一類的靜態成員。

此外,還有一種方法可以返回匿名類型,並將其轉換回接收方聲明的等效匿名類型。上了車,雖然:-)

例(read article)移動到C#:

// Method that returns anonymous type as object 
object ReturnAnonymous() 
{ 
    return new { City="Prague", Name="Tomas" }; 
} 

// Application entry-point 
void Main() 
{ 
    // Get instance of anonymous type with 'City' and 'Name' properties 
    object o = ReturnAnonymous(); 

    // This call to 'Cast' method converts first parameter (object) to the 
    // same type as the type of second parameter - which is in this case 
    // anonymous type with 'City' and 'Name' properties 
    var typed = Cast(o, new { City="", Name="" }); 
    Console.WriteLine("Name={0}, City={1}", typed.Name, typed.City); 
} 

// Cast method - thanks to type inference when calling methods it 
// is possible to cast object to type without knowing the type name 
T Cast<T>(object obj, T type) 
{ 
    return (T)obj; 
} 
相關問題