6

我在我的数据库模型之上有一个模型,并将对象映射到我的存储库中。

但是,显然,我是直接在我的 GetUsers 中“选择新”还是“选择工厂结果”,如下所示。我在运行时收到错误,即 CreateFromDbModel 方法没有转换为 sql (System.NotSupportedException)。

有没有解决的办法?我能以某种方式修复它吗?

想要使用工厂方法的原因是我可能会在其他地方实例化对象并希望将“映射代码”保留在一个地方......

感谢您的任何评论,安德斯

    public IQueryable<User> GetUsers(bool includeTeams)
    {
        return from u in _db.sc_Players
               where (includeTeams || (!u.aspnet_User.sc_Player.IsTeam))
               select UserFactory2.CreateFromDbModel(u);
    }


    public static User CreateFromDbModel(sc_Player player)
    {
        return new User
                   {
                       Id = player.sc_PlayerID,
                       FirstName = player.FirstName.Trim(),
                       LastName = player.LastName.Trim(),
                       PresentationName = player.FirstName.Trim() + " " + player.LastName.Trim(),
                       LoginName = player.aspnet_User.LoweredUserName,
                       IsTeam = player.IsTeam,
                       Email = player.aspnet_User.aspnet_Membership.Email,
                       Password = player.aspnet_User.aspnet_Membership.Password
                   };
    }
4

2 回答 2

1

问题是因为您在 GetUsers 方法中返回 IQueryable 吗?尝试返回 List 。这将强制 Linq 查询在方法内执行。

public List<User> GetUsers(bool includeTeams)
{
    return (from u in _db.sc_Players
    where (includeTeams || (!u.aspnet_User.sc_Player.IsTeam))
    select UserFactory2.CreateFromDbModel(u)).ToList();
}

Not sure if this will fix the problem, just a hunch. I was able to duplicate what you are doing in Linqpad on a local database, and it worked. But my sample was not returning an IQueryable. Are you furthur modifing the IQueryable collection outside of GetUsers()?

Edit:

I've done some more checking. I was able to duplicate the error only when I modified my sample so the IQueryable collection was used in a second Linq query after calling GetUsers():

IQueryable<User> query = GetUsers(true);

var q = from u in query
    where u.Name.Contains("Bob")
    select new {Name = u.FirstName + " " + u.LastName};

I bet if you will return a List as suggested above in GetUsers() the error will go away. The only down side is that any filtering you do after calling GetUsers() will not limit the amount of data returned from the database because you have already executed the query when calling .ToList().

Edit 2:

不幸的是,我认为没有其他方法可以将您的工厂方法包含在查询中。我还有一个想法。您可以为 IQueryable 创建一个扩展方法,将其称为 ToUserList()。在 ToUserList() 中,您在查询和返回用户集合的工厂方法上调用 ToList()。使用 Linq 完成数据过滤后调用此方法。这将允许您仅在准备好从数据库加载数据时执行查询。下面给出一个例子。

public static List<Users> ToUserList(this IQueryable<User> query)
{
     return query.ToList().Select(u => UserFactory2.CreateFromDbModel(u)); 
}

像这样调用扩展方法:

// Filter the data using linq. When you are ready to execute the query call:
query.ToUserList(); // Query will execute and a list of User objects returned.

希望这是有道理的。道格拉斯·H。

于 2009-09-10T04:50:54.680 回答
1

这个错误几乎解释了这一切。

“方法 CreateFromDbModel 没有转换为 sql (System.NotSupportedException)”

您的 CreateFromDbModel 方法不是 sql 函数。在对象从服务器专门返回给您之前,您的应用程序将无法运行 CreateFromDbModel 函数。您很可能必须在查询中调用 ToList() 或类似的东西,然后才能在它们上运行 CreateFromDbModel。

于 2009-09-10T04:59:22.887 回答