Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a dictionary collection, string, int. I want convert this dictionary to anonymous object with properties that are collection keys and anonymous object propertie's values are dictionary values. Is it a way to do it?
Thank you
Are you looking for this?
var keyVals = dict.Select(kv => new { Key = kv.Key, Value = kv.Value });
But i can't imagine a use case for this. Why do you prefer the anonymous type over the already available dictionary?
Like this :
from x in mydictionairy select new { anonymousKey = x.Key, anonymousValue = x.Value}
Related
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 1 year ago.
Improve this question
I'm new to C#.
I have a method that returns an object with the List of objects inside.
0 {{DapperRow, RecordedFormID = 'id1'}} object {Dapper.SqlMapper.DapperRow}
1 {{DapperRow, RecordedFormID = 'id2'}} object {Dapper.SqlMapper.DapperRow}
How can I get those ids as strings?
Here is a screenshot
You can use System.Linq namespace and do this:
yourObject.Select(x => x.RecordedFormID);
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
how do i get value(i) - value(i-1) (or compare adjacent ) in a data dictionary? I need to flag the adjecent values which are same in the same dictionary
Dictionaries are unordered. There is no position for the items in it, and as a result, there is no such thing as an "adjacent" item.
Basically, you shouldn't be doing this with a regular dictionary. Adjacency/indexing is not a concept for normal dictionaries. If you really need something, use an OrderedDictionary which is some sort of hybrid array/dictionary.
int? lastValue = null;
int diff;
foreach( KeyValuePair<int, int> kvp in myDictionary )
{
if(lastVaue != null)
diff = kvp.value - lastValue;
lastValue = kvp.value;
}
As stated by other foreach (GetEnumerator()) is loose in a Dictionary
If you want order you should use another collection.
OrderedDictionary
If the keys are ordered I think you could use
myDictionary.OrderBy(x => x.Key)
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
I have this two list here:
List<KeyValuePair<TextBox, KeyValuePair<string, Type>>> textbox1
So, i need that to get the Textbox i need to write:
textbox1.Key
What should i type to get the KeyValuePair<string, Type>> Type, like textbox1.Value.Value?
The type contains string or int.What i need is to access it's value so i can assign an if operator but i don't know how to. Then i need to modify it but that's my next step and i can arrange that myself.
I suppose textbox1.Key won´t compile since textbox1 is a List and therefor does not have any member called Key. Having said this the following may work for you:
textbox[i].Value.Value
Where i is the index of your KV-pair within the list (rather then the actual key)
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
I have comma-separated data in my column called col1, and I have an array of strings
IEnumerable<string> year = {"1990","1991","1992","1993","1994","1995","1996","1997","1998","1999","2000"}
I have tried the following
searchfrom = searchfrom.Where(x => years.Contains(x.col1.Replace(',', ' '))).ToList();
and
searchfrom = searchfrom.Where(x => years.Contains(x.col1)).ToList();
I want that row which is match any "year" into "col1"
To optimize such queries, you should first convert your years collection into a hash set.
var years = new HashSet<string>(new [] { "1990", "1991", ... });
In your Where clause, you need to split the contents of each of your records, x, for which you can use x.Split(','). Then, you need to check whether any of these subparts are contained within the years collection.
var result = searchfrom.Where(x =>
x.Split(',').Any(years.Contains)
).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 8 years ago.
Improve this question
I am trying to iterate through a list of objects and only change the ones that match a particular type. My current code looks like this. (Platform is an extension of Entity, and entities is a list of type Entity)
foreach (Platform p in entities.OfType<Platform>) { p.doStuff() }
I am getting the error "foreach cannot opperate on a 'method group'" Thanks for anyone's help. :)
Alright then :
foreach (Platform p in entities.OfType<Platform>())
//Will loop through all object of Platform type in entites.OfType<Platform>()
You can use LINQ and the "is" and "as" keywords.
foreach (object o in entities.Where(x => x is Platform))
{
Platform p = o as Platform;
p.doStuff();
}