How to convert an object having rows into a datatable [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have an object that has 14 objects inside it . Each object contains my row values. How do I put everyting into a datatable using C#

static DataTable GetTable(List<Object> yourObjectList)
{
// This is assuming you have a list of objects
var _firstObject = yourObjectList.First();
var table = new DataTable();
// Do this multiple times for each parameter you have.
table.Columns.Add(_firstObject.ParamaterName, typeof(string));
foreach(var obj in yourObjectList)
{
table.Rows.Add(obj.ParamaterName, obj.ParamaterName2, etc);
}
return table;
}
I'm assuming you have a list of objects with multiple properties. You need to add a column for each property to the table, then iterate over the list and add rows for each object.

datatable table= new datatable ();
foreach(var item in yourObjec)
{
table.Rows.Add(item.objectvalue);
}

Related

How to add items into the list from other list using for each C# smart technique [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Can I add list data from other list items using for each loop without extracting the method?
I want to add a new list to PersDriver.
Here is the code where PersDriver is a list of Test class and AutoDrivers is a list passing from the model. But I am getting an error at the end of the new PersDriver() {} that ; expected.
Can anyone correct my simple code without extracting a new method or add it to the list?
Test t1 = new Test(testModel model)
{
PersDriver = new List<PersDriver>()
{
model.AutoDrivers.ForEach(_driver =>
{
new PersDriver()
{
Id= _driver.Id,
Name = _driver.Name
}
}
}
}
You can try LINQ:
Test t1 = new Test(testModel model)
{
PersDriver = model.AutoDrivers.Select(_driver =>
new PersDriver()
{
Id= _driver.Id,
Name = _driver.Name
}).ToList()
};

How to know the type of object in list? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I created a list in C# with two object type, and I need to show the object with type ClientPhysique. ClientPhysique and ClientMoral extend the Client class.
public List<Client> Clients = new List<Client>();
#region Methodes
// Ajouter un Client:
public void Ajouter()
{
ClientPhysique CP = new ClientPhysique("EE111111", "Ahmed", "Yassine", 1, 06020202, "Rue Gmasa, Marrakech, 40000");
ClientMorale CM = new ClientMorale("A1414", "Rue mhamid, A6, Marrakech, 40160", 12121212, 1000000, 6, 06060606, "Titwan de Titwan");
Clients.Add(CP);
Clients.Add(CM);
}
You can narrow down your list with the LINQ Method OfType. It allows you to iterate the list items with a specific type, ignoring all other items:
foreach(ClientPhysique item in Clients.OfType<ClientPhysique>()) {
//Do something with your item
}
you could use Oftype as follows
var result = Clients.OfType<ClientPhysique>();
foreach (var element in result)
{
}

Update of one list automatically updates another list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have one original list and two temporary lists.
Based on certain condition, I am adding data from the original list(after modifying certain values) to these temp lists. But changes made in one temp list via this copy operation is updating the other temp list as well.
List<UserLMSSubFunc> lstUserLMSSubFunc = null;
List<UserLMSSubFunc> lstUserLMSSubFuncTemp1 = new List<UserLMSSubFunc>();
List<UserLMSSubFunc> lstUserLMSSubFuncTemp2 = new List<UserLMSSubFunc>();
foreach (Constructor subFnc in originalList)
{
foreach (KeyValuePair<string, string> kv in OriginalList)
{
if (kv.Value.ToUpper() == subFnc.SubFuncCode.ToUpper())
{
if (subFnc.FuncCode == null)
{
subFnc.FuncCode = kv.Key;
templist1.Add(subFnc);
}
else
{
subFnc.FuncCode = kv.Key;
Templist2.Add(subFnc);
}
}
}
}
The reason why the data in your lists is changing is due to the reference / value type mishmash. Your lists hold only a reference to an object inside that list. Therefore whenever you change your object which you have pulled from one of the lists I suspect it is the same object which is located in the other two as well, hence the change which seemingly propagates itself across the lists.
Use Setter
private var temp1 = new List<Package>();
public List<Package> temp1
{
set { temp1 = value;
update your temp2}
get { return temp1; }
}

How to get value from HashTable and call the function from this object? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I created Class Disk
Disk disk1 = new Disk();
Hashtable myCatalog = new Hashtable();
try
{
myCatalog.Add("Disk1", disk1);
}
catch
{
Console.WriteLine("An element with Key = \"Disk1\" already exists.");
}
Disk valueColl = (Disk)myCatalog.Values;
valueColl.
And here I have a problem.
How I can use this method ShowCompositions();
Values is a ICollection containing the values in the Hashtable.
You could do this.
Disk valueColl = (Disk)myCatalog["Disk1"]; // access element with `Key`
valueColl.ShowCompositions(); // this will work
On other note as #jamesthorpe highlighted in comments , try using Dictionary over Hashtable, it has added advantages.
Easiest way
Hashtable hashtable = new Hashtable();
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}

How can I get selected column into a Dictionary? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
var topLevel= resultRows.GroupBy(g => g["CustomerAccountType"]);
I want to select columns only like Actual1, Actual2, Actual3, etc. The names of the columns are not known upfront - they are composed dynamically.
I am not using a strongly type object because resultsRows is a constructed Dictionary from SQL cubes.
I want to dynamically add ActualN...to..ActualN to a collection as dictionary.
You can use Select to make a projection of Actual1, Actual2, Actual3:
var topLevel= resultRows
.Select(d => new Dictionary<string,object> {
{"Actual1", d["Actual1"]}
, {"Actual2", d["Actual2"]}
, {"Actual3", d["Actual3"]}
});
This will remove all columns other than Actual1, Actual2, Actual3 from your dictionary.
I want Actual1 to ActualN added dynamically without knowing how many are there
You can do that, too:
// Construct this list dynamically
var selection = new List<string> {
"Actual1", "Actual2", ..., "ActualN"
};
var topLevel= resultRows.Select(d => selection.ToDictionary(s => s, s => d[s]));

Categories

Resources