How to get list of one column values from DataTable?

You can use Linq to DataTable: var ids = dt.AsEnumerable().Select(r => r.Field<int>(“id”)).ToList(); UPDATE: Without Linq List<int> ids = new List<int>(dt.Rows.Count); foreach(DataRow row in dt.Rows) ids.Add((int)row[“id”]); Note for efficiency it’s better to use row[index] instead of row[columnName]. First one just gets column by index from columns array. Latter gets column index from internal dictionary which maps … Read more

Forwarding events in C#

Absolutely: class B { private A m_a = new A(); public event EventType EventB { add { m_a.EventA += value; } remove { m_a.EventA -= value; } } } In other words, the EventB subscription/unsubscription code just passes the subscription/unsubscription requests on to EventA. Note that this doesn’t allow you to raise the event just … Read more

ExecuteNonQuery requires the command to have a transaction error in my code

You need to change this line SqlCommand cmd = new SqlCommand(“update Contact_Info set CustInfo=” + ds.GetXml() + ” WHERE Customer_ID=” + a + “”, scon); in this way SqlCommand cmd = new SqlCommand(“update Contact_Info set CustInfo=” + ds.GetXml() + ” WHERE Customer_ID=” + a + “”, scon, sqlTrans); The error message states exactly the problem. … Read more

How to convert Dictionary to Dictionary in c#

Use the ToDictionary method: Dictionary<string, string> dString = dObject.ToDictionary(k => k.Key, k => k.Value.ToString()); Here you reuse the key from the original dictionary and you convert the values to strings using the ToString method. If your dictionary can contain null values you should add a null check before performing the ToString: Dictionary<string, string> dString = … Read more

Get Max value from List

Assuming you have access to LINQ, and Age is an int (you may also try var maxAge – it is more likely to compile): int maxAge = myTypes.Max(t => t.Age); If you also need the RandomID (or the whole object), a quick solution is to use MaxBy from MoreLinq MyType oldest = myTypes.MaxBy(t => t.Age);

How can I evaluate C# code dynamically?

Using the Roslyn scripting API (more samples here): // add NuGet package ‘Microsoft.CodeAnalysis.Scripting’ using Microsoft.CodeAnalysis.CSharp.Scripting; await CSharpScript.EvaluateAsync(“System.Math.Pow(2, 4)”) // returns 16 You can also run any piece of code: var script = await CSharpScript.RunAsync(@” class MyClass { public void Print() => System.Console.WriteLine(1); }”) And reference the code that was generated in previous runs: await script.ContinueWithAsync(“new … Read more