views:

66

answers:

2
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

Public Class 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

End Class

doesnt works!!!!

It Says 'Set' accessor of property 'SubcatName' is not accessible.

A: 

You can create a custom type and modify your select to instantiate that for the return. Take a look here: http://stackoverflow.com/questions/460831/linq-to-sql-return-from-function-as-iqueryablet

David
thanks but can you make an example here
Max_67
Um, ok. My VB may be a little syntactically off, but this is the general idea (note that you'll need to create the MyObject class, of course): Return From subcat In _dataContext.subcategories, cat In _dataContext.categories Where subcat.CategoryID = cat.CategoryID Select New MyObject() With {.SubCategoryID = subcat.SubCategoryId, .SubCategoryName = subcat.SubCategoryName, .CategoryID = subcat.CategoryID, .CategoryName = cat.CategoryName}
David
Public Function List() As ?????????????? will bePublic Function List() As MyObjectis right???
Max_67
That is correct. The idea that this is leaning towards is to use custom objects as your view models, rather than coupling your views to your data model directly.
David
David im concerned!!!!!!!! i edited post with my last code...help me
Max_67
Why are your setters private? Nothing outside your class will be able to set them if they're private.
David
indeed thanks David it works now
Max_67
A: 

Compiler is just telling you that you have declared Private Set on SubcatName and yet ypou are trying to assing a value to it after New Myobj().

For a first run you can declare a POD class (plain old data - just public data, no methods or properties) and once you see it runnung you can go tweaking it, adding methods etc.

If it's really important that all properties are read-only you'll need to try making your querying method a static member of the same class.

Also, there is a way to return anonymous type and cast it back to equivalent anonymous type declared at the receiving side. Got to move on to C# though :-)

Example (read article):

// 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;
}
ZXX