Ayyappa
Ayyappa

Reputation: 33

change my list of objects into dictionary key list objects using c#

I am generating list of objects now I need to change that list of objects with specific id inner that contains that specific id objects data by using dictionary.

This is my complete code.

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);
    }
}

I am returning list of objects but now I need to change every specific key(PID) inner that contains related objects.

Upvotes: 0

Views: 84

Answers (2)

Tolga Evcimen
Tolga Evcimen

Reputation: 7352

Way to go is this if you want to have a dictionary instead of list:

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;

Upvotes: 1

sujith karivelil
sujith karivelil

Reputation: 29006

From the question what I understand is CommObj is a List<ComRep> and you want to get the item to a dictionary that matches the specific key. if so you can try the following

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

Or you can try this, if you need a Dictionary instead for this list.

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;
                   });
}

Updates :

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

return groupedProducts, change the return type as Dictionary<string,List<ComRep>>

Upvotes: 1

Related Questions