【问题标题】:what type does my object query return?我的对象查询返回什么类型?
【发布时间】:2010-07-12 11:57:18
【问题描述】:
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”访问器不可访问。

【问题讨论】:

  • 问号容易,只要冷静、冷静、理性地思考,我们就会度过难关。

标签: asp.net-mvc linq-to-sql types


【解决方案1】:

您可以创建自定义类型并修改您的 select 以实例化它以返回。看这里:Linq To Sql return from function as IQueryable<T>

【讨论】:

  • 嗯,好的。我的 VB 在语法上可能有点偏离,但这是一般的想法(请注意,您当然需要创建 MyObject 类): 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}
  • 公用函数 List() As ??????????????将是 Public Function List() As MyObject 是对的???
  • 没错。这倾向于使用自定义对象作为您的视图模型,而不是直接将您的视图耦合到您的数据模型。
  • 为什么你的 setter 是私有的?如果它们是私有的,则您班级以外的任何人都无法设置它们。
【解决方案2】:

编译器只是告诉你你已经在 SubcatName 上声明了 Private Set,但是你试图在 New Myobj() 之后为其赋值。

对于第一次运行,您可以声明一个 POD 类(普通的旧数据 - 只是公共数据,没有方法或属性),一旦看到它运行,您就可以对其进行调整、添加方法等。

如果所有属性都是只读的非常重要,则需要尝试将查询方法设为同一类的静态成员。

此外,还有一种方法可以返回匿名类型并将其转换回接收方声明的等效匿名类型。不过必须继续使用 C# :-)

示例 (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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多