I am trying to work with Dictionary<> along with List<> for searching purpose. I know, I can do this easily with List<> as follows:
var con = (from c in db.Customers
where c.Status == status
select c).ToList();
But preferred and tried to implement the above with Dictionary<>. My concept (We all know that) is using the key/value will increase the search option performance. This looks simple and stuck a bit. Here what I've tried:
static void Main(string[] args)
{
Dictionary<string, Customer> custDictionary = new Dictionary<string, Customer>(); //Dictionary declared
List<Customer> lst = new List<Customer>(); //List of objects declared
Customer aCustomer = new Customer(); //Customer object created
/**Assign values - Starts**/
aCustomer.CustomerId = 1001;
aCustomer.CustomerName = "John";
aCustomer.Address = "On Earth";
aCustomer.Status = "Active";
aCustomer.CustomerId = 1002;
aCustomer.CustomerName = "James";
aCustomer.Address = "On Earth";
aCustomer.Status = "Inactive";
/**Assign values - Ends**/
custDictionary.Add(aCustomer.Status, aCustomer); //Added to the dictionary with key and value
string status = Console.ReadLine().ToUpper();
if (custDictionary.ContainsKey(status)) //If key found in the dictionary
{
Customer cust = custDictionary[status];
Console.WriteLine(cust.CustomerId + " " + cust.CustomerName); //Outputs the final result - Right now no result found here
}
Console.ReadKey();
}
public class Customer
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string Address { get; set; }
public string Status { get; set; }
}
Unfortunately, the above doesn't return any result. What I am trying is to get customer details by passing status key and again passed Customer object as the value. I am not sure what I am missing here.
One more thing, in real-life projects, we get database results as list. So in this scenario, if Dictionary<> is used, I believe, the database results should be kept as follows:
lst.Add(aCustomer); //As database will have more result or data simply
On the other hand, I believe, the dictionary should look like the below:
Dictionary<string, List<Customer>> custDictionary = new Dictionary<string, List<Customer>>();
My question - Is it a good idea to pass a list of objects in the dictionary for the key/vale pair and I've tried using so. But didn't get the output yet.
Note: This sounds like a novice question and yes, it's. I've tried to search online and still studying it. I apology to ask such a question and would expect some answers if there are any better ways to do the above.
UPDATED
If you want to store them in a list, you can do the following code. To select the items, you can then use Linq, and in this way you don't have the issue of duplicate values in a dictionary:
var lst = new List<Customer>(); //List of objects declared
lst.AddRange(
new List<Customer>() {
new Customer()
{
CustomerId = 1001,
CustomerName = "John",
Address = "On Earth",
Status = "Active"
},
new Customer()
{
CustomerId = 1002,
CustomerName = "James",
Address = "On Earth",
Status = "Inactive"
}
}
);
var status = Console.ReadLine();
var selected = lst.Where(x => x.Status.ToUpper() == status.ToUpper()).ToList();
foreach (var item in selected)
{
Console.WriteLine(item.CustomerId + " " + item.CustomerName);
}
UPDATE 2
If you want to add the above list in a dictionary, you can do as follows:
var custDictionary = new Dictionary<string, List<Customer>>();
// the above code for the list
custDictionary.Add("keyname", lst);
ORIGINAL ANSWER
You are saving one customer only since you are overwriting the first customer with the second one:
Dictionary<string, Customer> custDictionary = new Dictionary<string, Customer>();
List<Customer> lst = new List<Customer>();
// Add first customer
var aCustomer = new Customer()
{
CustomerId = 1001,
CustomerName = "John",
Address = "On Earth",
Status = "Active"
};
custDictionary.Add(aCustomer.Status.ToUpper(), aCustomer);
// Add second customer
var bCustomer = new Customer()
{
CustomerId = 1002,
CustomerName = "James",
Address = "On Earth",
Status = "Inactive"
};
custDictionary.Add(bCustomer.Status.ToUpper(), bCustomer);
Also you need to store the Status as uppercase, since you are checking if the status exist in uppercase:
string status = Console.ReadLine().ToUpper();
if (custDictionary.ContainsKey(status)) //If key found in the dictionary
{
Customer cust = custDictionary[status];
Console.WriteLine(cust.CustomerId + " " + cust.CustomerName); //Outputs the final result - Right now no result found here
}
Console.ReadKey();
If you already has the list and want to create a Dictionary<string, List<Customer>> you can do it with this:
Dictionary<string, List<Customer>> dict =
list.GroupBy(c=>c.Status.ToUpper()).ToDictionary(g => g.Key, g=> g.ToList());
And iterate it:
foreach (var customer in dict[status.ToUpper()])
{
}
But,
I don't see the value in doing so. if you need to get all the customers with a specific status stay with what you have - a simple linq query.
Even if you are adding status as key, there are 2 problems with your code.
You need to create 2 objects to create 2 customers, one by one. You are adding customer only once, and assigning values twice.
Console.ReadLine().ToUpper() - Remove the ToUpper() since you are adding values in mixed case. If you want to do that, initialize dictionary with StringComparer.InvariantCultureIgnoreCase.
This will work for you.
Dictionary<string, Customer> custDictionary = new Dictionary<string, Customer>(StringComparer.InvariantCultureIgnoreCase); //Dictionary declared
List<Customer> lst = new List<Customer>(); //List of objects declared
Customer aCustomer = new Customer(); //Customer object created
/**Assign values - Starts**/
aCustomer.CustomerId = 1001;
aCustomer.CustomerName = "John";
aCustomer.Address = "On Earth";
aCustomer.Status = "Active";
custDictionary.Add(aCustomer.Status, aCustomer); //Added to the dictionary with key and value
Customer bCustomer = new Customer(); //Customer object created
bCustomer.CustomerId = 1002;
bCustomer.CustomerName = "James";
bCustomer.Address = "On Earth";
bCustomer.Status = "Inactive";
custDictionary.Add(bCustomer.Status, bCustomer); //Added to the dictionary with key and value
string status = Console.ReadLine().ToUpper();
if (custDictionary.ContainsKey(status)) //If key found in the dictionary
{
Customer cust = custDictionary[status];
Console.WriteLine(cust.CustomerId + " " + cust.CustomerName); //Outputs the final result - Right now no result found here
}
Console.ReadLine();
First of all your dictionary key should be customerId not status. it will be a good practice to check if dictionary contains the key other wise it will throw the exception already same key is added. so its better to check then perform add or update in the dictionary.
static void Main(string[] args)
{
Dictionary<string, Customer> custDictionary = new Dictionary<string, Customer>(); //Dictionary declared
List<Customer> lst = new List<Customer>(); //List of objects declared
Customer aCustomer = new Customer(); //Customer object created
/**Assign values - Starts**/
aCustomer.CustomerId = 1001;
aCustomer.CustomerName = "John";
aCustomer.Address = "On Earth";
aCustomer.Status = "Active";
if (!custDictionary.ContainsKey(aCustomer.CustomerId))
custDictionary.Add(aCustomer.CustomerId, aCustomer);
else
custDictionary[aCustomer.CustomerId] = aCustomer;
aCustomer.CustomerId = 1002;
aCustomer.CustomerName = "James";
aCustomer.Address = "On Earth";
aCustomer.Status = "Inactive";
/**Assign values - Ends**/
if (!custDictionary.ContainsKey(aCustomer.CustomerId))
custDictionary.Add(aCustomer.CustomerId, aCustomer);
else
custDictionary[aCustomer.CustomerId] = aCustomer;
string status = Console.ReadLine().ToUpper();
if (custDictionary.ContainsKey(aCustomer.CustomerId)) //If key found in the dictionary
{
Customer cust = custDictionary[aCustomer.CustomerId];
Console.WriteLine(cust.CustomerId + " " + cust.CustomerName); //Outputs the final result - Right now no result found here
}
Console.ReadKey();
}
Your are not getting any output because your are converting your input to uppercase while you have insert the keys in pascalcase and in case of C# collections keys are case sensitive. in this way your input does not matched to any key in collection
change your line number : 29 to this code
string status = Console.ReadLine();
and insert "Inactive" from you console this key exist in your collection
so you will desired result..
Related
When I am Trying to save current list of data into database, I need to get already existing data from database, and need to compare with current list of data.
I have two lists one is PreviousList(existing data from DB) and other is CurrentList(Modified data)
public class SoftClose
{
public int ID = -1;
public int AID = -1;
public int WFID = -1;
public string PREFIX;
public DateTime SCDATE;
public string STATUS;
}
In CurrentList I modified Prefix to D2 where ID=1 and added new row(Id=4)...
My req is
When I am trying to save CurrentList to Db,
If there is any new Prefix in CurrentList that is not there in PreviousList I need to insert that new row and need to change Status to ADD for that row.
I changed Prefix to D2 where Id = 1 in CurrentList. D1 is there is DB and but not in CurrentList so i need to delete it. So i need to change the status to DELETE for that record. I should not insert D2 record where id=1 becuase D2 is already there. If I changed to D5 where Id = 1 then I need to insert it because D5 is not there in DB So i need to change the status to UPDATE.
How to do this? What is the best approach to compare lists
here is a solution you could try:
List<SoftClose> previousList = new List<SoftClose>(){
new SoftClose(){ID=1, Status = "NO_CHANGE",AID="19", Prefix = "D1"},
new SoftClose(){ID=2, Status = "NO_CHANGE",AID="20", Prefix = "D2"},
new SoftClose(){ID=3, Status = "NO_CHANGE",AID="21", Prefix = "D3"}
};
List<SoftClose> currentList = new List<SoftClose>(){
new SoftClose(){ID=1, Status = "NO_CHANGE",AID="19", Prefix = "D2"},
new SoftClose(){ID=2, Status = "NO_CHANGE",AID="20", Prefix = "D2"},
new SoftClose(){ID=3, Status = "NO_CHANGE",AID="21", Prefix = "D6"},
new SoftClose(){ID=4, Status = "NO_CHANGE",AID="22", Prefix = "D4"},
new SoftClose(){ID=5, Status = "NO_CHANGE",AID="22", Prefix = "D5"}
};
var addlist = currentList.Where(c => previousList.All(p => !p.ID.Equals(c.ID) && !p.Prefix.Equals(c.Prefix)));
foreach(var n in addlist)
{
var index = currentList.FindIndex(p => p.Prefix.Equals(n.Prefix));
currentList[index].Status = "ADD";
}
var updateORdeletelist = currentList.Where(c => c.Status.Equals("NO_CHANGE") && previousList.Exists(p => p.ID.Equals(c.ID) && !p.Prefix.Equals(c.Prefix)));
foreach (var n in updateORdeletelist)
{
var index = currentList.FindIndex(p => p.Prefix.Equals(n.Prefix));
if (previousList.FindIndex(p => p.Prefix.Equals(n.Prefix)) < 0)
currentList[index].Status = "UPDATE";
else
currentList[index].Status = "DELETE";
}
foreach (var item in currentList)
{
Console.WriteLine($"Id:{item.ID}, Desc1:{item.Prefix}, Status:{item.Status}");
}
output
Id:1, Desc1:D2, Status:DELETE
Id:2, Desc1:D2, Status:NO_CHANGE
Id:3, Desc1:D6, Status:UPDATE
Id:4, Desc1:D4, Status:ADD
Id:5, Desc1:D5, Status:ADD
There is a tool called Side by Side SQL Comparer in C# at https://www.codeproject.com/Articles/27122/Side-by-Side-SQL-Comparer-in-C.
basic use of the component:
using (TextReader tr = new StreamReader(#"c:\1.sql"))
{
sideBySideRichTextBox1.LeftText = tr.ReadToEnd();
}
using (TextReader tr = new StreamReader(#"c:\2.sql"))
{
sideBySideRichTextBox1.RightText = tr.ReadToEnd();
}
sideBySideRichTextBox1.CompareText();
You load the left and right sides to their respective variables sideBySideRichTextBox1.LeftText and sideBySideRichTextBox1.RightText and compare them with sideBySideRichTextBox1.CompareText();
In your case the 1.sql and 2.sql would be your PreviousList and CurrentList -database files.
There is more detailed documentation at the project-site.
I'm developing an app with Entity Framework.
I've got a combo box with the names of the tables in the database.
I have the following code:
string table = cbTables.SelectedItem.ToString();
using (var dbContext = new Entities()) {
if (table.Equals("Person")) {
List<Person> list = (from l in dbContext.People select l).ToList();
} else if (table.Equals("Student")) {
List<Student> list = (from u in dbContext.Student select u).ToList();
} else if (table.Equals("Grade")) {
List<Grade> list = (from p in dbContext.Grade select p).ToList();
}
how can i avoid all these if-else checks?
Is it possible to get the name of a class from a string holding the name?
example:
string = "Person";
var str = //something
List<str> list = (from u in dbContext.str select u).ToList();
ComboBox is capable of displaying dictionary through the datasource, so you can bind display text with the data you actually want instead of checking the display text. Then in this case we would bind it to the type of the entity:
Dictionary<string,Type> typeMap = new Dictionary<string,Type> {
{ "Person", typeof(Person) },
{ "Student", typeof(Student) },
{ "Grade", typeof(Grade) },
}
then bind it to ComboBox like:
cbTables.DataSource = new BindingSource(typeMap, null);
cbTables.DisplayMember = "Key";
cbTables.ValueMember = "Value";
then, when you need to get selected entity, use
Type entityType = (Type) cbTables.SelectedValue;
DbSet set = dbContext.Set(entityType);
However, after this you need to inspect entityType and display the form accordingly. If your form needs List e.g. List, then use
List<Student> studentList = set.Cast<Student>.ToList();
Assuming you don't want a switch but you want something dynamic, you can use queries instead:
using (var context = new Context())
{
var people = context.Database.SqlQuery<Object>(
"select * from People").ToList();
}
To expand on my comment:
You can declare a dictionary to map from the string of your table name to the actual DbSet that is your table:
string table = cbTables.SelectedItem.ToString();
using (var dbContext = new Entities()) {
Dictionary<string, DbSet> mapping = new Dictionary<string, DbSet> {
{ "Person", dbContext.Person },
{ "Student", dbContext.Student },
{ "Grade", dbContext.Grade }
};
//...
The problem will remain - what to do with your result set? Right now you have typed lists with the specific type of each table's members; you won't be able to do that without a decision (if/switch) of some sort.
Why aren't you using Switch for this purpose?
string table = cbTables.SelectedItem.ToString();
using (var dbContext = new Entities())
{
switch (table)
{
case"Person":
List<Person> list = (from l in dbContext.People select l).ToList();
break;
case"Student":
List<Student> list = (from u in dbContext.Student select u).ToList();
break;
case"Grade":
List<Grade> list = (from p in dbContext.Grade select p).ToList();
break;
}
}
Is there an way of storing both the ID and Test ID values in the Dropdown list but I need to display the DisplayName? I need both of these values when the item is selected
ID | TestID | DisplayName
1 | 2 | Test
Sample Code:
ddList.DataSource = DvName;
ddList.DataTextField = "DisplayName";
ddList.DataValueField = "ID"; // + "TestID" ???
You could use a delimiter to put both values together and then delimit them once you pull them out.
ddList.DataValueField = obj.ID + "~" + obj.TestID;
Otherwise you could create a dictionary that hold the ID and the Test ID, so that when you select your item from the dropdown, you can use the value field as the key to the dictionary and select the TestID.
private Dictionary<string, string> referenceValues = new Dictionary<string, string>();
referenceValues.Add("ID", "TestID");
ddList.DataValueField = "ID";
//When pulling then value back
var referencedItem = referenceValues[ddList.selectedValue];
Assuming you're getting this values from a model you could do something like:
public string NewValue
{
get
{
return ID+ "," + TestId;
}
}
ddList.DataSource = DvName;
ddList.DataTextField = "DisplayName";
ddList.DataValueField = "NewValue";
Assuming that you are using a class for each instance of your data; add another property to the class and bind to that property:
public class MyObject
{
//other stuff
public string DisplayValue
{
get { return String.Format("{0} ({1})", ID, TestID); }
}
}
ddList.DataSource = DvName;
ddList.DataTextField = "DisplayName";
ddList.DataValueField = "DisplayValue";
I have a string with a message containing some fields I want to swap out to actual values
var message = "Hi [CustomerName]. Its [TODAY], nice to see you were born on the [DOB]!";
var mappingCodes = new List<string> {"[CUSTOMER_NAME]","[DOB]",[TODAY]};
var customEmails = new Dictionary<string, string>();
var today = DateTime.Now;
var customers = new List<Customer>()
{
new Customer()
{
FirstName = "Jo",
LastName = "Bloggs",
Email = "jo#bloggs.com",
DOB = "10/12/1960"
}
};
foreach (var customer in customers)
{
var emailMessage = "";
customEmails.Add(customer.Email,emailMessage);
}
What I'm trying to do is loop through each of the customers and take the message replacing any of the mappingCodes with actual codes.
e.g. [Today] Would be the today and CustomerName would be Customer.FirstName + Customer.LastName
There could be 1000's of customers so I need something robust. I'm just not sure how to first check the string contains any of the mappingCodes and then replace them with the desired values.
Any advice?
You could try something like this. String.Format is rather efficient. It also would allow you to format Date.Today, if you want.
var customers = new List<Customer>()
{
new Customer()
{
FirstName = "Jo",
LastName = "Bloggs",
Email = "jo#bloggs.com",
DOB = "10/12/1960"
}
};
foreach (var customer in customers)
{
var emailMessage = String.Format("Hi {0}. Its {1}, nice to see you were born on the {2}!", customer.FirstName, DateTime.Today, customer.DOB);
customEmails.Add(customer.Email,emailMessage);
}
You can use Regex.Replace(string, MatchEvaluator):
var customers = new[] {
new {
Name = "Fred Flintstone",
City = "Bedrock"
},
new {
Name = "George Jetson",
City = "Orbit City"
}
};
string template = "Hello, [NAME] from [CITY]!";
var re = new Regex(#"\[\w+\]"); // look for all "words" in square brackets
foreach (var customer in customers)
{
Trace.WriteLine(
re.Replace(template, m => {
// determine the replacement string
switch (m.Value) // m.Value is the substring that matches the RE.
{
// Handle getting and formatting the properties here
case "[NAME]":
return customer.Name;
case "[CITY]":
return customer.City;
default:
// The "mapping code" is not supported, I just return the
// original substring
return m.Value;
}
}));
}
Obviously the above is just the general approach, you'll have to modify it to support your mapping codes and data structures.
Is it possible to flatten a one-to-many relationship using dynamic LINQ?
For example, I might have a list of Users and the User class contains a list of many UserPreferences. The UserPreference class is essentially a name/value pair.
A user will define what types of user preferences are available for a group of users.
public class User
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public IList<UserPreference> UserPreferences
{
get;
set;
}
}
public class UserPreference
{
public UserPreference(string name, object userValue)
{
this.Name = name;
this.UserValue = userValue;
}
public string Name
{
get;
set;
}
public object UserValue
{
get;
set;
}
}
Therefore one user group might be defined in the following way:
List<User> users = new List<User>();
User user1 = new User();
user1.FirstName = "John";
user1.LastName = "Doe";
user1.UserPreferences.Add(new UserPreference("Favorite color", "Red"));
User user2 = new User();
user2.FirstName = "Jane";
user2.LastName = "Doe";
user2.UserPreferences.Add(new UserPreference("Favorite mammal", "Dolphin"));
user2.UserPreferences.Add(new UserPreference("Favorite color", "Blue"));
users.Add(user1);
users.Add(user2);
return users;
The desired output would be:
First Name Last Name Favorite Color Favorite Mammal
John Doe Red NULL
Jane Doe Blue Dolphin
Is there a way to create an anonymous type so that UserPreferences would get rolled up into the User?
For example,
var u = UserScopedSettingAttribute.Select("new (FirstName as FirstName, UserValue as FavoriteColor)", null);
string name = u.FirstName;
string color = u.FavoriteColor;
Ultimately this list of Users will get bound to an ASP.NET GridView web control. There will be a large volume of data involved in this operation and performance will be critical.
Any suggestions are appreciated!
I know it doesn't exactly answer your question, but compiling strings into new classes at runtime like dlinq does has always had kind of a bad smell to it. Consider just simply using a DataTable like this,
DataTable prefs = new DataTable();
IEnumerable<DataColumn> cols = (from u in users
from p in u.UserPreferences
select p.Name)
.Distinct()
.Select(n => new DataColumn(n));
prefs.Columns.Add("FirstName");
prefs.Columns.Add("LastName");
prefs.Columns.AddRange(cols.ToArray());
foreach (User user in users)
{
DataRow row = prefs.NewRow();
row["FirstName"] = user.FirstName;
row["LastName"] = user.LastName;
foreach (UserPreference pref in user.UserPreferences)
{
row[pref.Name] = pref.UserValue;
}
prefs.Rows.Add(row);
}
This should do it. Flattening is generally done with SelectMany extension method, but in this case I am using a let expression. The code to remove the null preferences is a bit ugly and could prob be improved but it works:
var flattenedUsers = from user in GetUsers()
let favColor = user.UserPreferences.FirstOrDefault(pref => pref.Name == "Favorite color")
let favMammal = user.UserPreferences.FirstOrDefault(pref => pref.Name == "Favorite mammal")
select new
{
user.FirstName,
user.LastName,
FavoriteColor = favColor == null ? "" : favColor.UserValue,
FavoriteMammal = favMammal == null ? "" : favMammal.UserValue,
};
My best suggestion would be to not use dynamic LINQ, but add a flatuser class and then loop through the users. The code for this is simple, and if you were able to get a linq query with similar results it would generate the same code, although you can't really tell how optimized it would be as it might involve some joins that would incur a performance penalty instead of just looping. If you were pulling this from a database using LINQ to SQL then you could use an entity relation to to get the data using linq instead of this loop.
Loop:
List<FlatUser> flatusers = new List<FlatUser>();
foreach (User u in users)
{
foreach (UserPreference up in u.UserPreferences)
{
flatusers.Add(new FlatUser
{
FirstName = u.FirstName,
LastName = u.LastName,
Name = up.Name,
UserValue = up.UserValue
});
}
}
Flat User Class:
public class FlatUser
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public string Name
{
get;
set;
}
public object UserValue
{
get;
set;
}
}
Unfortunately
var u = UserScopedSettingAttribute.Select("new {FirstName as FirstName, UserValue as FavoriteColor}", null);
string name = u.FirstName;
string color = u.FavoriteColor;
won't work. When you use DLINQ Select(string) the strongest compile time class information you have is Object, so u.FirstName will throw a compile error. The only way to pull the properties of the runtime generated anonymous class is to use reflection. Although, if you can wait, this will be possible with C# 4.0 like this,
dynamic u = UserScopedSettingAttribute.Select("new {FirstName as FirstName, UserValue as FavoriteColor}", null);
string name = u.FirstName;
string color = u.FavoriteColor;
I think the pragmatic answer here is to say your attempting to force C# to become a dynamic language and any solution is going to be really pushing C# to its limits. Sounds like your trying to transform a database query of columns that are only determined at query time into a collection that is based on those columns and determined at run time.
Linq and Gridview binding is really pretty and succinct and all but you have to start thinking about weighing the benefit of getting this compiler bending solution to work just so you don't have to dynamically generate gridview rows and columns by yourself.
Also if your concerned about performance I'd consider generating the raw HTML. Relying on the collection based WebForms controls to efficiently display large sets of data can get dicey.
You add in a couple of OnItemDataBound events and boxing and unboxing is going to really gum up the works. I'm assuming too your going to want to add interactive buttons and textboxes to the rows as well and doing 1000 FindControls has never been fast.
There are probably more efficient ways to do this, but to actually answer your question, I came up with the following code. (Note that I've never worked with DynamicLinq before, so there may be a better way to use it to accomplish your goal.)
I created a console application, pasted in the classes from your post, then used the following code.
static void Main(string[] args)
{
var users = GetUserGroup();
var rows = users.SelectMany(x => x.UserPreferences.Select(y => new { x.FirstName, x.LastName, y.Name, y.UserValue }));
var userProperties = rows.Select(x => x.Name).Distinct();
foreach (var property in userProperties)
{
Console.WriteLine(property);
}
Console.WriteLine();
// The hard-coded variety.
var results = users.Select(x => new
{
x.FirstName,
x.LastName,
FavoriteColor = x.UserPreferences.Where(y => y.Name == "Favorite color").Select(y => y.UserValue).FirstOrDefault(),
FavoriteAnimal = x.UserPreferences.Where(y => y.Name == "Favorite mammal").Select(y => y.UserValue).FirstOrDefault(),
});
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The dynamic variety.
DynamicProperty[] dynamicProperties = new DynamicProperty[2 + userProperties.Count()];
dynamicProperties[0] = new DynamicProperty("FirstName", typeof(string));
dynamicProperties[1] = new DynamicProperty("LastName", typeof(string));
int propIndex = 2;
foreach (var property in userProperties)
{
dynamicProperties[propIndex++] = new DynamicProperty(property, typeof(string));
}
Type resultType = ClassFactory.Instance.GetDynamicClass(dynamicProperties);
ConstructorInfo constructor = resultType.GetConstructor(new Type[] {});
object[] constructorParams = new object[] { };
PropertyInfo[] propInfoList = resultType.GetProperties();
PropertyInfo[] constantProps = propInfoList.Where(x => x.Name == "FirstName" || x.Name == "LastName").OrderBy(x => x.Name).ToArray();
IEnumerable<PropertyInfo> dynamicProps = propInfoList.Where(x => !constantProps.Contains(x));
// The actual dynamic results creation.
var dynamicResults = users.Select(user =>
{
object resultObject = constructor.Invoke(constructorParams);
constantProps[0].SetValue(resultObject, user.FirstName, null);
constantProps[1].SetValue(resultObject, user.LastName, null);
foreach (PropertyInfo propInfo in dynamicProps)
{
var val = user.UserPreferences.FirstOrDefault(x => x.Name == propInfo.Name);
if (val != null)
{
propInfo.SetValue(resultObject, val.UserValue, null);
}
}
return resultObject;
});
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Display the results.
var displayResults = dynamicResults;
//var displayResults = results;
if (displayResults.FirstOrDefault() != null)
{
PropertyInfo[] properties = displayResults.First().GetType().GetProperties();
int columnWidth = Console.WindowWidth / properties.Length;
int index = 0;
foreach (PropertyInfo property in properties)
{
Console.SetCursorPosition(index++ * columnWidth, Console.CursorTop);
Console.Write(property.Name);
}
Console.WriteLine();
foreach (var result in displayResults)
{
index = 0;
foreach (PropertyInfo property in properties)
{
Console.SetCursorPosition(index++ * columnWidth, Console.CursorTop);
Console.Write(property.GetValue(result, null) ?? "(null)");
}
Console.WriteLine();
}
}
Console.WriteLine("\r\nPress any key to continue...");
Console.ReadKey();
}
static List<User> GetUserGroup()
{
List<User> users = new List<User>();
User user1 = new User();
user1.FirstName = "John";
user1.LastName = "Doe";
user1.UserPreferences = new List<UserPreference>();
user1.UserPreferences.Add(new UserPreference("Favorite color", "Red"));
user1.UserPreferences.Add(new UserPreference("Birthday", "Friday"));
User user2 = new User();
user2.FirstName = "Jane";
user2.LastName = "Doe";
user2.UserPreferences = new List<UserPreference>();
user2.UserPreferences.Add(new UserPreference("Favorite mammal", "Dolphin"));
user2.UserPreferences.Add(new UserPreference("Favorite color", "Blue"));
users.Add(user1);
users.Add(user2);
return users;
}