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]));
Related
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()
};
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 2 years ago.
Improve this question
I have code looping over data in a JSON file and so based on conditions of wildcard for the first, OR Second OR Third variable , I was doing the regex, then writing it out and I really don't like the way it looks.
So readability is important and performance, but data integrity and error handling etc.. are as well
What is a better way to write this?
for loop....
var firstWildCard = vidaBiliy.SponsorPackage.Where(s => Regex.IsMatch(s.ExternalPolicy, $"^.*{Regex.Escape(HcdpPmtFctrCd)}{Regex.Escape(hccCpmtFctrCd)}$")).FirstOrDefault()?.PackageID;
if(firstWildCard == null)
{
var secondWildCard = vidaBiliy.SponsorPackage.Where(s => Regex.IsMatch(s.ExternalPolicy, $"^{Regex.Escape(hcdpPlnCvgCD)}.*{Regex.Escape(hccCpmtFctrCd)}$")).FirstOrDefault()?.PackageID;
if(secondWildCard == null)
{
var thirdWildCard = vidaBiliy.SponsorPackage.Where(s => Regex.IsMatch(s.ExternalPolicy, $"^{Regex.Escape(hcdpPlnCvgCD)}{Regex.Escape(HcdpPmtFctrCd)}.*$")).FirstOrDefault()?.PackageID;
}
}
check this out
var patterns = new string[]{
$"^.*{Regex.Escape(HcdpPmtFctrCd)}{Regex.Escape(hccCpmtFctrCd)}$",
$"^{Regex.Escape(hcdpPlnCvgCD)}.*{Regex.Escape(hccCpmtFctrCd)}$",
$"^{Regex.Escape(hcdpPlnCvgCD)}{Regex.Escape(HcdpPmtFctrCd)}.*$"
}
var wildcards = new List<String>();
foreach (var pattern in patterns)
{
var matchResult = vidaBiliy.SponsorPackage.Where(s => Regex.IsMatch(s.ExternalPolicy, pattern)).FirstOrDefault()?.PackageID;
wildcards.Add(matchResult);
if (matchResult != null)
break;
}
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)
{
}
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 7 years ago.
Improve this question
I have a problem with the algorithm Fisher in c#.
I have a class system of SystemDecyzyjny. In it I have a dictionary.
public Dictionary<int, Dictionary<int, double>> posortowane = new Dictionary<int, Dictionary<int, double>>();
Data in dictionary:
How to display with posortowane data in Form1.cs in this form:
2 4 1 3
. . . .
. . . .
How to refer to the dictionary and display data from it attributes?
If you want to access the value of the key 0, then you just write this:
posortowane[0]
This returns a Dictionary<int, double>.
Now you can read the value with key 0 of the latter dictionary as:
posortowane[0][0]
The value with the key 1 as:
posortowane[0][1]
and so on and so forth.
It sounds like you want to print out all keys of the inner dictionary. This can be done in the following way:
foreach (var outerKey in posortowane.Keys)
{
foreach (var inner in posortowane[outerKey])
{
Console.WriteLine(inner.Key);
}
}
If you want to print out the both the keys and values of the inner dictionary you could do:
foreach (var outerKey in posortowane.Keys)
{
foreach (var inner in posortowane[outerKey])
{
Console.WriteLine($"{inner.Key}:{inner.Value}");
}
}
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);
}