如何使用实体框架和linq c#获得两个表组合记录?

时间:2017-03-10 12:07:18

标签: c# entity-framework linq

我希望使用linq c#lambda表达式获取两个表组合数据,并在列表中获取列数get的修订号。  我试过这段代码,但我不知道如何用linq lambda表达式编写查询。

这是我的方法:

public ActionResult GetUserList(string searchRequest)
{          
    List<User> userList = new List<User>();              
    if (searchRequest != null)
    {
        if (searchRequest == "All")
        {
// here i want to write select query but how i don't know
            userList = db.user.ToList();
        }
        else if (searchRequest == "Flight") 
        {
            userList = db.user
                .Where(t => t.type_id == (int)ServiceTypeEnum.Flight)
                .ToList();
        }                   
    }                                       
    return Json(new { data = userList });
}

任何人都有关于此查询的想法,请告诉我怎么做。

3 个答案:

答案 0 :(得分:1)

为要选择的列

定义UserDto类
public class UserDto
{
    public int Id{get;set;}

    public int Name{get;set;}

    //Other Properties
}

然后将您的代码更改为以下

public ActionResult GetUserList(string searchRequest)
{
    try
    {                            
        if (searchRequest != null)
        {
            IQueryable<User> query; 

            if (searchRequest == "All")
            {
                query = db.user.AsQueryable(); // here i want to write select query but how i don't know
            }
            else if (searchRequest == "Flight") 
            {
                UserList = db.user.Where(t => t.type_id == (int)ServiceTypeEnum.Flight);
            }  

            if(query != null)
            {
                var list = query.Select(e=> new UserDto
                {
                    Id = e.Id,
                    Name = e.Name
                    //Other properties
                }).ToList();

                return Json(new { data = list });
            }   
        }                                       

   }
   catch (Exception ex)
   {
       throw;
   }
   return Json(null);
}

答案 1 :(得分:1)

您将在lambda中选择特定字段时遇到的问题是结果通常是匿名类型。来自两个不同查询的匿名类型不能轻易地在列表中连接在一起,因为编译器无法验证类型的结构或相等性。

  

还有其他方法......

最佳实践方法是创建一个正式的类型定义并使用它,以便您可以在lambda表达式之外操作对象。请注意,我假设了一个简单的示例结构,它是用户的子集:

public ActionResult GetUserList(string searchRequest)
{
    try
    {
        List<UserSearchResult> UserList = new List<UserSearchResult>();
        if (searchRequest != null)
        {
            if (searchRequest == "All")
            {
                UserList.AddRange(db.user.Select(u => new UserSearchResult { Title = u.Title, FirstName = u.Firstname, LastName = u.Lastname })); // here i want to write select query but how i don't know
            }
            else if (searchRequest == "Flight")
            {
                UserList.AddRange(db.user.Where(t => t.type_id == (int)ServiceTypeEnum.Flight)
                    .Select(u => new UserSearchResult { Title = u.Title, FirstName = u.Firstname, LastName = u.Lastname }));
            }
        }
        return Json(new { data = UserList });
    }
    catch (Exception ex)
    {
        throw;
    }
    return Json(null);
}

public class UserSearchResult
{
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

因为我们已经将我们选择的特定字段的结果显式地转换为正式类型,所以我们现在可以在查询之外的操作中使用该类型,甚至可以操作这些值。

答案 2 :(得分:0)

我认为局部变量阻碍了你。只需返回您想要的结果。

public ActionResult GetUserList(string searchRequest)
{
    if (searchRequest == "All")
    {
       var users = db.user
           .Select(user => new {user.Name, user.Address.ZipCode})
           .ToList();

       return ToJson(users);
    }
    else if (searchRequest == "Flight") 
    {
        List<User> users = db.user
            .Where(t => t.type_id == (int)ServiceTypeEnum.Flight)
            .ToList();

        return ToJson(users);
    }

    return ToJson(new List<User>());
}

private ActionResult ToJson<T>(T list)
{
    return Json(new { data = list });
}