如何从函数返回通用列表(Of T)

时间:2014-02-16 22:15:08

标签: asp.net asp.net-mvc vb.net

我在我的班级中有以下功能,我想将其作为通用列表返回。

Public Function ProductTitlePrice(Of T)() As List(Of T) Implements IProductRepository.ProductTitlePrice

    Dim v = (From F In _db.Fstock Where F.GUID = 2
        Select F.STK_NAME, F.SELL_PRICE
        ).ToList


    Return v

End Function

我得到的错误是......

Error   2   Value of type 'System.Collections.Generic.List(Of <anonymous type>)' cannot be converted to 'System.Collections.Generic.List(Of T)'.

基本上,我想从函数中返回包含数据的通用列表,然后我将在Controller中调用此函数并将其数据映射到ViewModel。

修改

根据以下评论,似乎不可能。我在UI层中拥有所有的ViewModel,我想要做的就是从我的DAL层返回数据,然后在我的UI层中处理这些数据。

我的DAL函数将连接多个表中的数据,这就是我希望依赖ViewModel的原因。

例如,如果我的DAL中的函数具有以下代码

var query = from ug in ent.userGroups
            join u in ent.Users on ug.OwnerUserId = ug.userID
            select new
            {
                Name = u.UserName,
                Id = u.userID
                Group = ug.GroupName
            };

如何从我的函数返回查询,然后在我的UI图层中处理输出。

1 个答案:

答案 0 :(得分:1)

我最终得到了以下解决方案。

  • Styxxy建议的DTO新项目

  • 然后在我的UL中使用Automapper将DTO映射到我的ViewModels。

以下是来源:http://bengtbe.com/blog/2009/04/14/using-automapper-to-map-view-models-in-asp-net-mvc/