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 1 year ago.
Improve this question
I got a List<Person> listA -> where Person has
string name
int age
List<string> jobs
I need to remove from listA all items that jobs.count() < 1, using linq, any ideas?
You shouldn't mutate a list using LINQ, because it's designed for querying, however, List<T> already has a RemoveAll method as part of it's protocol:
listA.RemoveAll(person => person.Jobs.Count < 1);
RemoveAll accepts a Predicate<T> delegate which should return true when an item is to be removed.
However, if you're intention is to create a new list that only contains people with less than one job, you can use LINQ:
var newList = listA
.Where(person => person.Jobs.Count < 1)
.ToList();
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 basically want to go through the 2d List<> and deposit the sum of the items in a column to a 1d List. So far have a double for loop but I cant figure out the logic. Any help with how I can start to contruct it logically would be helpful. Thank you. This is the code I have so far:
So if you have this:
var grid = new List<List<int>> {
new() { 1,2 },
new() { 3,4 }
}
You can do:
grid.Pivot().Select(c => c.Sum());
And you'll get an enumerable that is { 4, 6 } (1+3 and 2+4)
You can get Pivot from fubo's answer here (or choose any one of the other suggested transforms)
If I misunderstood and you wanted {3,5} just skip the Pivot part
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
How to return a person's name when we search for a hobby and it should return empty when there are no matching hobbies.
There are many ways to skin this cat. Using Linq is probably the most standard.
The operation you want to do is to select the Key of the dictionary where the value (the array of hobbies) contains the string "Yoga"
val keys = hobbies.Where(keyvalue => keyvalue.contains("Yoga")).Select(keyvalue => keyvalue.key);
now you have a sequence of keys where one of the hobbies is Yoga.
If there is only a single one, and it's an error if there are more, get CC with keys.Single().
If there could be zero or more, and you just want an arbitrary one, you want keys.FirstOrDefault() which returns null if there isn't one.
In between you have SingleOrDefault() for 0 or 1, or First() for 1 or more select an arbitrary one.
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 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")
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));