Reorder a List of string using LINQ [closed] - c#

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 9 years ago.
Improve this question
I have a list of string List which contains all state names.
I need to move few states(Ex New York, California etc) to appear at the top of the list.
How can I do this using LINQ?
FYI: the list is alreday sorted in alphabetical order.
Its just a simple List and few important states needs to be at the top. No criteria.

The following is a proof of concept using letters instead of states.
var all = new List<string>() {"a","b","c","d","e"};
var top = new List<string>() {"c","d"};
var finalList = top.Concat(all.Except(top));
The idea is that you have your full list of states (all) and a list of the ones you want at the top ('top'). You then take the ones at the top and concatenate the list of the remaining ones that you create using the Except method.

List<string> importantStates = {...};//It's up to you
var list = states.OrderBy(x=>!importantStates.Contains(x));

Related

Dictionary lists is not adding the new lists c# linq [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 9 days ago.
The community reviewed whether to reopen this question 8 days ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I really don't understand why stackoverflow, keeps closing my question as if I already know the answer. That's the reason I am asking.
Add details and clarify the problem you’re solving. This will help others answer the question. You can edit the question or post a new one.
I do have a problem with the logic itself, I don't need to add the whole model and view models to ask how to add or retrieve a dictionary.
The idea is to add about 9 different LISTS,
List<int>All`
trying to add a dictionary. or whatever helps me to retain those 9 lists and a key for each.
Dictionary<int, List<IDS>> CollectionID = new Dictionary<int,List<IDS>>();
Here's what I have:
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(result);
All = myDeserializedClass.Data;
foreach (var item in All)
{
var id = item.ID;
Dictionary<int, List<IDS>> CollectionID = new Dictionary<int,List<IDS>>();
CollectionID.Add(id, All); //is not adding.
}
Question 1. How can I capture the id located inside List<int>>All and add it as the key for the dictionary without the use foreach()?
Question 3. How do I retrieve such a dictionary by using the key?
var allIds = JsonConvert.DeserializeObject<List<IDS>>
the images are only to show that the list is getting add it, however, the count is always 1, so it should be 9.

Is there any easy way to clone all elements of a list inside itself? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
Improve this question
For example,
new List<int>{1,2,3,4} -> new List<int>{1,2,3,4,1,2,3,4}
Of course, I know it can be easy with a loop. But is there any more efficient way to do this?
You can use .AddRange() method,
List<int> input = new List<int>(){1,2,3,4};
var result = new List<int>(input);
result.AddRange(input);
Console.WriteLine(string.Join(", ", result));
If you want to include in the same List, then you can directly apply .AddRange() to input list itself.
input.AddRange(input);
If you want to modify the existing list, you can do;
var list = new List<int>{1,2,3,4};
list.AddRange(list.ToList());
Note that it is necessary to copy the list (with ToList() or some other way), otherwise you end up modifying the same list while AddRange iterate over it to add elements.
If you want to create a new list, use Concat
var list = new List<int>{1,2,3,4};
var newList = list.Concat(list).ToList();
You are looking for AddRange:
list.AddRange(new List<yourtype>(list));
In the code above I have cloned list because under the hood, there must be a loop that traverses list's elements and add them to itself. So, in theory I can imagine cases of looping the list and infinitely adding the newly added elements, but when you do actual coding, it's worth testing whether
list.AddRange(list);
also works well. If so, then cloning the list is unnecessary. If not, then you need to clone.

C# Multidimensional string Lists [closed]

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 6 years ago.
Improve this question
For my knowledge I want to understand about multidimensional string Lists in C#. Please can some one explain me with good example code of creating multidimensional string Lists and also retrieving data from it.
I want examples for the following
List<string[]>
List<string[,]>
List<List<string>>
Declare them just like any other list type:
List<string[]> list1 = new List<string[]>();
List<string[,]> list2 = new List<string[,]>();
List<List<string>> list3 = new List<List<string>>();
and access them like so:
// First and third use identical indexers:
string s1 = list1[0][0];
string s3 = list3[0][0];
// Second is similar, just with a multidimensional indexer instead
string s2 = list2[0][0,0];

Usinq Linq to get rows based on comma-separated value [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
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();

Find what number a string is in a 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 8 years ago.
Improve this question
I want to be able to know what number a string is in one list so I can match it with a string in another list.
Something like:
myList[1] would be 1
myList2[5] would be 5
Sorry if I'm not clear, but this is really hard to explain.
The number you're referring to is called the index.
You can find the index of a string in your list by using the List<T>.IndexOf() method.
int indexOfFoo = myList.IndexOf("foo");
To find the index of a string in a list of strings:
var myList = new List<string> { "x", "y", "z" };
var indexOfY = myList.IndexOf("y");
You can use myList.IndexOf("string") to get the index of the item in your list.
http://msdn.microsoft.com/en-us/library/e4w08k17(v=vs.110).aspx
I'm not sure if that is what you're asking, but that is how I understood it.
try it:
yourList.IndexOf("urstring")

Categories