[C#] एक IENumerable <KeyValuePair <>> से एक शब्दकोश को पुन: प्रयास करना


Answers

IENumerable का उपयोग कर डेटाटेबल से एक डिक्शनरी ऑब्जेक्ट बनाना

using System.Data;
using ..


public class SomeClass {
    //define other properties
    // ...  

    public Dictionary<string, User> ConvertToDictionaryFromDataTable(DataTable myTable, string keyColumnName)
        {
            // define IEnumerable having one argument of KeyValuePair
            IEnumerable<KeyValuePair<string,User>> tableEnumerator = myTable.AsEnumerable().Select(row => 
                {
                    // return key value pair
                    return new KeyValuePair<string,User>(row[keyColumnName].ToString(),                        
                        new User 
                        {
                            UserID=row["userId"].ToString(),
                            Username=row["userName"].ToString(),
                            Email=row["email"].ToString(),
                            RoleName=row["roleName"].ToString(),
                            LastActivityDate=Convert.ToDateTime(row["lastActivityDate"]),
                            CreateDate=Convert.ToDateTime(row["createDate"]),
                            LastLoginDate=Convert.ToDateTime(row["lastLoginDate"]),
                            IsActive=Convert.ToBoolean(row["isActive"]),
                            IsLockedOut=Convert.ToBoolean(row["isLockedOut"]),
                            IsApproved=Convert.ToBoolean(row["isApproved"])
                        });
                });
            return tableEnumerator.ToDictionary(x => x.Key, x => x.Value);
        }

}


public class User
{

    public string UserID { get; set; }       
    public string Username { get; set; }
    public string Email { get; set; }
    public string RoleName { get; set; }       
    public DateTime LastActivityDate { get; set; }
    public DateTime CreateDate { get; set; }
    public DateTime LastLoginDate { get; set; }
    public bool IsActive { get; set; }
    public bool IsLockedOut { get; set; }
    public bool IsApproved { get; set; }                

// Other methods to follow..
}



        IEnumerable<KeyValuePair<string,User>> ieUsers = membershipUsers.AsEnumerable().Select(row =>
            {
                return new KeyValuePair<string,User>(row.UserName.ToString(),
                    new User
                    {
                        Username = row.UserName.ToString(),
                        Email = row.Email.ToString()
                    });
            });
        allMembershipUsers = ieUsers.ToDictionary(x => x.Key, x => x.Value);
        return allMembershipUsers;
    }
}
Question

मेरे पास एक तरीका है जो एक IEnumerable<KeyValuePair<string, ArrayList>> , लेकिन कुछ कॉलर्स को विधि के परिणाम की आवश्यकता होती है। मैं IEnumerable<KeyValuePair<string, ArrayList>> को एक Dictionary<string, ArrayList> कैसे परिवर्तित कर सकता हूं ताकि मैं TryGetValue उपयोग कर TryGetValue ?

तरीका:

public IEnumerable<KeyValuePair<string, ArrayList>> GetComponents()
{
  // ...
  yield return new KeyValuePair<string, ArrayList>(t.Name, controlInformation);
}

फोन करने वाले:

Dictionary<string, ArrayList> actual = target.GetComponents();
actual.ContainsKey("something");



Links