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

Posted

技术标签:

【中文标题】如何使用 List<object> 将对象值添加到 viewmodel【英文标题】:How to add object values to viewmodel with List<object> 【发布时间】:2019-10-17 10:01:08 【问题描述】:

我有一个视图模型并试图返回一个 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;
        
    

【问题讨论】:

不确定你在问什么。我没有看到任何列表。你能详细说明你想要做什么吗? 公开列表 QuestionResponseIds get;放; 是列表对象,请查看视图模型。 请不要在可以复制代码的时候发截图,这样会更难帮助你 【参考方案1】:

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

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;

【讨论】:

QuestionResponseIds 的类型为 list,当我输入“ba”时。我没有看到 QuestionResponseIds,因为结果将来自数据库。我已经添加了图片。 我正在使用 linq to sql 类,我已附加该类,如果有帮助请告诉我。 你发布了私有支持字段,我只能猜测公共属性是什么。 我添加了一个预期的结果,我不确定如何从 linq 获取公共属性到 sql 类。 @AnanthAsamani 稍微更新了答案,但无法完整查看所有涉及的模型,我无能为力

以上是关于如何使用 List<object> 将对象值添加到 viewmodel的主要内容,如果未能解决你的问题,请参考以下文章

如何使用提供程序将 List<object> 存储在 sharedpreferences 中?

java 如何将List<Object[]>转换成List<实体>?

如何将两个List<Map<String,Object>>合并为一个

如何将 List<KeyValuePairs<int,object>> 保存为 SQLite 表?

如何将map<string list<>>转换成城map<string,object>

list<String>如何转化为Map<String, Object>,list<EmpVO>如何转化为Map<String, Object>