【问题标题】:change my list of objects into dictionary key list objects using c#使用 c# 将我的对象列表更改为字典键列表对象
【发布时间】:2017-02-03 05:12:57
【问题描述】:

我现在正在生成对象列表,我需要使用字典更改具有特定 id 内部的对象列表,其中包含特定 id 对象数据。

这是我的完整代码。

m_Query = m_Query + " LEFT JOIN LIBRARY lib5 ON lib5.LIBRARY_ID = comm.STATUS2";
if (comObj.ProductsID != "" || comObj.ProductsID != null)
{
    m_Query = m_Query + " where prod1.PRODUCT_ID in ";
    string allproducts = string.Empty;
    allproducts = comObj.ProductsID;
    m_Query = m_Query + " ("+ allproducts + ") order by prod1.PRODUCT_ID desc";
}

DataSet dsPck = new DataSet();
dsPck = conn.GetDataSet(m_Query, CommandType.Text, ConnectionState.Open);
DataTable dt = new DataTable();
dt = dsPck.Tables[0];

if (dt.Rows.Count > 0)
{
    for (int i = 0; i<dt.Rows.Count; i++)
    {
        CommitmentReport Info = new CommitmentReport();
        Info.PRODUCT_ID = Convert.ToInt64(dt.Rows[i]["PRODUCT_ID"].ToString());
        Info.ProductName = dt.Rows[i]["ProductName"].ToString();
        Info.COMMITMENT_ID = Convert.ToInt64(dt.Rows[i]["COMMITMENT_ID"].ToString());
        Info.ReferenceNo = dt.Rows[i]["ReferenceNo"].ToString();
        Info.CreatedDate = Convert.ToDateTime(dt.Rows[i]["CreatedDate"].ToString());
        Info.Numbers = dt.Rows[i]["Numbers"].ToString();
        Info.Catalogue = dt.Rows[i]["Catalogue"].ToString();
        Info.Specification = Convert.ToInt64(dt.Rows[i]["Specification"].ToString());
        Info.Type = dt.Rows[i]["Type"].ToString();
        Info.SubmissionType = dt.Rows[i]["SubmissionType"].ToString();
        Info.ApprovalStatus = dt.Rows[i]["ApprovalStatus"].ToString();
        Info.Country = dt.Rows[i]["Country"].ToString();
        Info.Status = dt.Rows[i]["Status"].ToString();
        //  Info.Count = x;
        commLst.Add(Info);
    }
}

我正在返回对象列表,但现在我需要更改包含相关对象的每个特定键(PID)内部。

【问题讨论】:

  • 你的问题不清楚。您到底在寻找什么?样本输入/输出?
  • 需要将我的对象列表更改为字典键列表
  • 输入是多个 PID。需要输出的列表是: PID1:相关对象。 PID2:相关对象等
  • 所以你是说有相同PRODUCT_ID的项目,你想按他们的PRODUCT_ID分组?
  • 是的。正确的 。我需要的输出是这样的。

标签: c# asp.net dictionary


【解决方案1】:

从我理解的问题来看,CommObjList&lt;ComRep&gt;,您希望将该项目放入与特定键匹配的字典中。如果是这样,您可以尝试以下方法

string ProductID = "PID1";
var selectedProduct = CommObj.Where(x=> x.ProductID == ProductID)
                             .ToDictionary(y=> y.ProductID, y=> y); 
var allProducts = CommObj.ToDictionary(y=> y.ProductID, y=> y);  // all items to dictionary

如果您需要一个字典来代替此列表,您也可以试试这个。

Dictionary<string,ComRep> ComRepDict = new Dictionary<string,ComRep>();
for (int i = 0; i < dt.Rows.Count; i++)
{
    if(!ComRepDict.ContainsKey(dt.Rows[i]["PID"].ToString())
    {
      ComRepDict.Add( dt.Rows[i]["PID"].ToString(), new ComRep(){
                      PID = dt.Rows[i]["PID"].ToString();
                      PName = dt.Rows[i]["PName"].ToString();
                      PDesc = dt.Rows[i]["PDesc"].ToString();
                      commitmentid = dt.Rows[i]["commitmentid"].ToString();
                      country = dt.Rows[i]["country"].ToString();
                      status = dt.Rows[i]["status"].ToString();
                     Count = x;
                   });
}

更新:

 var groupedProducts = CommObj.GroupBy(x=> x.ProductID)
                              .ToDictionary(y=> y.Key, y=> y.ToList());

返回groupedProducts,将返回类型改为Dictionary&lt;string,List&lt;ComRep&gt;&gt;

【讨论】:

  • 请查看我更改的代码。请您尝试更改我的代码并返回分组对象
  • 我仍然没有得到你的实际要求。你试过Dictionary&lt;string,ComRep&gt;并返回ComRepDict 将方法的返回类型更改为Dictionary&lt;string,ComRep&gt;
  • 我有多个 productid。我需要返回那个分组的对象。像 ProductID1 :对象与 productID1 相关。 productID2 :对象与 productID2 相关。我需要那样回来。对相关产品进行分组
  • @Ayyappa:查看我帖子中的更新
  • 无法将类型 'System.Collections.Generic.Dictionary>' 隐式转换为 'System.Collections.Generic.List' EX_BusinessLayer D:\EX\Code\EX_BusinessLayer\Actions\CommitmentReportActions.cs
【解决方案2】:

如果你想要一本字典而不是列表,方法是这样的:

var dictionary = new Dictionary<string, List<ComRep>>();

if (dt.Rows.Count > 0)
{
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        ComRep Info = new ComRep();
        ....
        /// Your object initialization here

        if(!dictionary.ContainsKey(Info.PID))
            dictionary.Add(Info.PID, new List<ComRep>{ Info });
        else
            dictionary[Info.PID].Add(Info);
    }
}

return dictionary;

【讨论】:

  • 请您尝试更改我的代码并返回分组对象
  • 返回字典时显示错误。字典已声明但从未使用过。
  • 需要返回分组列表
  • 我的代码中唯一的错误是 return 关键字,我打错了。现在它已经修复了。试过了,工作。
  • 无法将类型 'System.Collections.Generic.Dictionary>' 隐式转换为 'System.Collections.Generic.List' EX_BusinessLayer D:\EX\Code\EX_BusinessLayer\Actions\Commit‌​mentReportActions.cs
猜你喜欢
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多