You will need to first initialize the dictionary in the constructor of the user class. Use the private set
to prevent people from re-initializing the dictionary.
# User.cs
namespace LDIFMod
{
public class User
{
User()
{
UserDict = new Dictionary<string, string>()
}
public string UserHash { get; set; }
public string UserID { get; set; }
public Dictionary<string, string> UserDict { get; private set; }
}
}
Your calling code becomes
var query = from line in File.ReadAllLines(args[0])
let UserRecord = line.Split(',')
select new User()
{
UserHash = UserRecord[2].Trim() +UserRecord[3].Trim(),
UserID = UserRecord[4].Trim(),
UserDict.Add(UserRecord[5],UserRecord[9]);
}
This returns one dictionary per query row. if you want all of the rows to share a dictionary you will need to make it static or not store it inside User
. If you do this be aware that linq is delayed execution so the dictionary will not be fully populated until after you fully enumerate the query.
I thought I would give a example of how to do it with all of them in a single dictionary.
# User.cs
namespace LDIFMod
{
public class User
{
public string UserHash { get; set; }
public string UserID { get; set; }
public readonly string[] SourceData {get; private set;}
}
}
and here is the query
var query = from line in File.ReadAllLines(args[0])
let UserRecord = line.Split(',')
select new User()
{
UserHash = UserRecord[2].Trim() + UserRecord[3].Trim(),
UserID = UserRecord[4].Trim(),
SourceData = UserRecord;
}
var UserLookp = query.ToDictionary((user) => user.SourceData[5], (user) => user.SourceData[9]);
This is purely from memory without a ide to check for bugs so there could be some errors.