实体框架返回空值

时间:2013-04-18 17:23:21

标签: c# entity-framework

我正在尝试将我的WCF服务列表返回给客户端。

我正在使用Entity Framework来查询我的数据库

我的Entity Framework库中有以下代码:

public List<Users> getUsersByLastName(string pLastName)
{
    using (var context = new AMTEntitiesContainer())
    {
        var users = context.Users
                           .Where((c) => c.LastName.Contains(pLastName))
                           .ToList();
        return users;
    }
}

这就是我在WCF中捕获结果并返回它的方式:

public List<wcfUser> getUsersByLastName(string pLastName)
{
    UserMethods userMethods = new UserMethods();
    List<Users> usersList = userMethods.getUsersByLastName(pLastName);
    List<wcfUser> usersListForClient = new List<wcfUser>();
    wcfUser usersForClient = new wcfUser();
    foreach (Users u in usersList)
    {
        usersForClient = new wcfUser();
        TranslateServerUserToClientUser(u, usersForClient);
        usersListForClient.Add(usersForClient);
    }
    return usersListForClient;
}

问题是,由于某种原因,usersList总是空的..为什么呢?

我通过将相同的查询抛入LINQPad

来检查数据库是否为空

1 个答案:

答案 0 :(得分:1)

这解决了它,显然DB正在返回一些奇怪的东西,但这抓住了它:

private AMTEntitiesContainer context = new AMTEntitiesContainer();

public IEnumerable<Users> getUsersByLastName(string pLastName)
{
    IQueryable<Users> results;

    results = (from m in context.Users
               where m.LastName.StartsWith(pLastName)
               select m);

    return results;
}