如何使用List <object>将对象值添加到viewmodel

时间:2019-05-31 18:54:49

标签: c# entity-framework linq asp.net-web-api

enter image description here我有一个视图模型,并试图返回linq对象,但不确定如何在linq返回查询中添加列表对象。 我已附上示例代码,以更好地理解。

查看模型:

public class BusinessAnalysisViewModel
{
    public Guid UserGuid { get; set; }
    public List<BAQandR> QuestionResponseIds { get; set; }
    public DateTime? CreateDate { get; set; }
    public DateTime UpdateDate { get; set; }
}
public class BAQandR
{
    public int BAQuestionId { get; set; }
    public string BAResponse { get; set; }
    public int? BAFinPreviousYr { get; set; }
    public int? BAFinCurrentYr { get; set; }
}

Linq查询:

public BusinessAnalysisViewModel GetBusinessAnalysisInfo(Guid userID)
    {
        using (var ctxAdmin = new MemberDataContext(ConfigurationManager.ConnectionStrings[Constants.CONFIG_KEY_MEMBER_CONNECTION_STRING].ToString()))
        {
            var model = (from ba in ctxAdmin.Business_Analysis
                         where ba.User_Guid == userID
                         select new BusinessAnalysisViewModel
                         {
                             UserGuid = ba.User_Guid,
                             CreateDate = ba.CreateDate,
                             UpdateDate = ba.UpdateDate,

                             QuestionResponseIds =
                         });
            return model;
        }
    }

[![在此处输入图片描述] [2]] [2]

[![在此处输入图片描述] [2]] [2]

我期望从我的逻辑得出以下json结果。

{
  "UserGuid": "000000000-4D3B-4598-A1FC-6B7B1D6D99D2",
  "CreateDate": "2019-05-22T18:52:14.458Z",
  "UpdateDate": "2019-05-22T18:52:14.458Z",
  "QuestionResponseIds": [
    {
      "BAQuestionId": 1,
      "BAResponse": 1,
      "BAFinPreviousYr": "",
      "BAFinPreviousYr": ""
    },
    {
      "BAQuestionId": 1,
      "BAResponse": 1,
      "BAFinPreviousYr": "",
      "BAFinPreviousYr": ""
    },
    {
      "BAQuestionId": 1,
      "BAResponse": 1,
      "BAFinPreviousYr": "",
      "BAFinPreviousYr": ""
    },
    {
      "BAQuestionId": 1,
      "BAResponse": 1,
      "BAFinPreviousYr": "",
      "BAFinPreviousYr": ""
    }
  ]  
}

我更改了模型,如下所示,

public class UserBusinessAnalysis
{
    public Guid UserGuid { get; set; }
    public DateTime? CreateDate { get; set; }
    public DateTime UpdateDate { get; set; }
    public int BAQuestionId { get; set; }
    public string BAResponse { get; set; }
    public int? BAFinPreviousYr { get; set; }
    public int? BAFinCurrentYr { get; set; }        
}

Linq如下所示,

public List<UserBusinessAnalysis> GetBusinessAnalysisInfoByUserId(Guid userID)
    {
        using (var ctxAdmin = new MemberDataContext(ConfigurationManager.ConnectionStrings[Constants.CONFIG_KEY_MEMBER_CONNECTION_STRING].ToString()))
        {
            var model = (from ba in ctxAdmin.Business_Analysis
                         where ba.User_Guid == userID
                         select new UserBusinessAnalysis
                         {
                             UserGuid = ba.User_Guid,
                             CreateDate = ba.CreateDate,
                             UpdateDate = ba.UpdateDate,
                             BAQuestionId = ba.B_Analysis_Question_id,
                             BAResponse = ba.B_Analysis_Response,
                             BAFinPreviousYr = ba.Fin_Review_Previous_Year,
                             BAFinCurrentYr = ba.Fin_Review_Current_Year
                         }).ToList();
            return model;
        }
    }

1 个答案:

答案 0 :(得分:0)

您应该能够简单地将list属性添加到结果中

using (var ctxAdmin = new MemberDataContext(ConfigurationManager.ConnectionStrings[Constants.CONFIG_KEY_MEMBER_CONNECTION_STRING].ToString()))
{
    var model = (from ba in ctxAdmin.Business_Analysis
                 where ba.User_Guid == userID
                 select new BusinessAnalysisViewModel
                 {
                     UserGuid = ba.User_Guid,
                     CreateDate = ba.CreateDate,
                     UpdateDate = ba.UpdateDate,

                     QuestionResponseIds = ba.QuestionResponseIds
                 });
    return model;
}

如果您的类型不匹配,则需要进行另一次投影

using (var ctxAdmin = new MemberDataContext(ConfigurationManager.ConnectionStrings[Constants.CONFIG_KEY_MEMBER_CONNECTION_STRING].ToString()))
{
    var model = (from ba in ctxAdmin.Business_Analysis
                 where ba.User_Guid == userID
                 select new BusinessAnalysisViewModel
                 {
                     UserGuid = ba.User_Guid,
                     CreateDate = ba.CreateDate,
                     UpdateDate = ba.UpdateDate,

                     QuestionResponseIds = ba.QuestionResponseIds.Select(r => new BAQandR { /* copy fields here */ }).ToList()
                 });
    return model;
}