How to remove x items from collection using LINQ? - c#

Is there a way to remove all items except first one from any type of collection (Control.Items, List ....) using LINQ only ?

No. LINQ is designed for querying collections (no side-effects), not for adding or removing items.
What you can do is write a query that takes the first element of the collection:
var result = source.Take(1);
Note that LINQ doesn't work with all types of collections; you need a LINQ provider to make LINQ work. For instance, source must implement IEnumerable<T> to use the extension methods of the Enumerable Class (LINQ-to-Objects).

How about something using reflection?
static void RemoveButFirst(object o){
Type t = o.GetType();
System.Reflection.MethodInfo rm = t.GetMethod("RemoveAt",
new Type[]{typeof(int)});
System.Reflection.PropertyInfo count = t.GetProperty("Count");
for (int n = (int)(count.GetValue(o,null)) ; n>1; n--)
rm.Invoke(o, new object[]{n-1});
}
This would work any time your collection exposed an int Count property and a RemoveAt(int) method, which I think those collections should.
And a more concise version, using dynamic, if you work with C# 4.0:
public static void RemoveBut(dynamic col, int k){
for (int n = col.Count; n>k; n--)
col.RemoveAt(n-1);
}

You can use .Take(1), but it returns a new collection, and leaves the original intact.
The idea of LINQ came from functional programming where everything is immutable, because of that, they didn't make it possible to modify the collections with LINQ.
Jon Skeet has a comment on the subject: LINQ equivalent of foreach for IEnumerable<T>

How about (in linq):
var result = list.Where(l => l != list.First());
But this would be better:
var result = list.Take(1);

List<string> collection = new List<string>();
collection.RemoveAll(p => p.StartsWith("something"));

listXpto.Where(x=>true /* here goes your query */)
.Select(x=>{listXpto.Remove(x); return null})
But I don´t know the real utility of that.
Remember that the remove method is for ILists, not IQueryable in general.

Related

LINQ: Transforming items in a collection

Is there a LINQ method to modify items in a collection, such as simply setting a property of each item in a collection? Something like this:
var items = new []{ new Item { IsActive = true } }
var items = items.Transform(i => i.IsActive = false)
where Touch enumerates each item and applies the transformation. BTW, I am aware of the SELECT extension method, but this would require I expose a method on the type that does this transformation and return the same reference.
var items = items.Select(i => i.Transform())
where Item.Transform returns does the transformation and return the same instance.
TIA
No, there are no methods in standard LINQ that allows you to modify items in a collection. LINQ is for querying collections and not for causing side-effects (e.g., mutating the items). Eric Lippert goes into the idea in more detail in his blog post: “foreach” vs “ForEach”.
Just use a loop.
foreach (var item in items)
{
item.IsActive = false;
}
LINQ is for querying. Use a simple loop if you want to modify. Just use the right tool for the right job. LINQ is not a messiah for everything.
There's a ForEach() on List, so you can do items.ToList().ForEach(i => i.IsActive = false). You might want to read this though.
The documentation page on MSDN for the Enumerable class lists all LINQ methods, and unfortunately no method there does what you want. LINQ is a query language and is not intended to modify collections. It is functional in its nature, meaning that it doesn't modify the collection it operates on, rather it returns a new enumerable.
For your purposes it is better to simply use a foreach loop, or if you feel the need write your own extension method to do what you want, eg.
public static void ForEach<T>(this IEnumerable<T> seq, Action<T> action)
{
foreach (T item in seq)
action(item);
}
which could then be used as you wanted:
items.ForEach(i => i.IsActive = false)

How to get an empty list of a collection?

I have a collection of anonymous class and I want to return an empty list of it.
What is the best readable expression to use?
I though of the following but I don't think they are readably enough:
var result = MyCollection.Take(0).ToList();
var result = MyCollection.Where(p => false).ToList();
Note: I don't want to empty the collection itself.
Any suggestion!
Whats about:
Enumerable.Empty<T>();
This returns an empty enumerable which is of type T. If you really want a List so you are free to do this:
Enumerable.Empty<T>().ToList<T>();
Actually, if you use a generic extension you don't even have to use any Linq to achieve this, you already have the anonymous type exposed through T
public static IList<T> GetEmptyList<T>(this IEnumerable<T> source)
{
return new List<T>();
}
var emp = MyCollection.GetEmptyList();
Given that your first suggestion works and should perform well - if readability is the only issue, why not create an extension method:
public static IList<T> CreateEmptyCopy(this IEnumerable<T> source)
{
return source.Take(0).ToList();
}
Now you can refactor your example to
var result = MyCollection.CreateEmptyCopy();
For performance reasons, you should stick with the first option you came up with.
The other one would iterate over the entire collection before returning an empty list.
Because the anonymous type there is no way, in source code, to create a list. There is, however, a way to create such list through reflection.

LINQ DynamicLibrary: How to extract count and list from IQueryable

I have started experimenting a bit with the LINQ DynamicLibrary. I am trying to replace a bunch of LINQ statements with a few or just one Dynamic LINQ query. My existing static query is as follows:
private List<TicketChartData> GenerateImpl(IList<ServiceItem> myList)
{
var output = from ticket in myList
group ticket by ticket.Region into grouped
orderby grouped.Count() descending
select new TicketChartData(grouped.Key, grouped.Count(), grouped.ToList());
return output.ToList();
}
As you see, my unit of work is ServiceItem.
This works all fine and gets me a result set grouped by Region. Using DynamicLibrary my attempt is to be able to group by any valid dynamic field (I will handle any validations separately). So, I tried writing the same query using DynamicLibrary, but am not very successful. Here is the new method which of course doesn't compile:
private List<TicketChartData> GenerateImpl(IList<ServiceItem> myList, string field)
{
IQueryable<ServiceItem> queryableList = myList.AsQueryable<ServiceItem>();
IQueryable groupedList = queryableList.GroupBy(field,"it").
OrderBy("Key descending").
Select("new (Key as Key)"); // Not what i need
return output.ToList();
}
I have been unable to extract neither the Count nor the List from the grouping. How do I do this? I have spent considerable amount of time looking for a solution. If possible I want to be able to avoid using Reflection. I have had some pointers in this front at the following link, but it doesn't help my actual problem. Thanks in advance for any help.
System.LINQ.Dynamic: Select(" new (...)") into a List<T> (or any other enumerable collection of <T>)
For Count it can be written as following:
private List GenerateImpl(IList myList, string field)
{
IQueryable queryableList = myList.AsQueryable();
IQueryable groupedList = queryableList.GroupBy(field,"it").
OrderBy("Key descending").
Select("new (Key as Key, Count() as Count)");
// Note: IQueryable doesn't have ToList() implementation - only IEnumerable
return output.ToList(); // will not work
}
For List - possible you'll need to add your custom implementation to DynamicLibrary...
See how it was done for Contains() method here
A solution is to use the .QueryByCube LINQ extension method provided by my product AdaptiveLINQ.
Disclaimer: I'm the AdaptiveLINQ developer

What is the best way to cast from ArrayList to List in .Net 2.0

I have a ArrayList of type BookingData to List<BookingData> ?
I am using .net 2.0 so i cannot use arrayList.Cast<int>().ToList() , and I dont want to make here foreach loop , do you have better ideas ?
Thanks.
Do note that something is going to have to enumerate the array-list to construct the List<T>; its only a matter of whether you do it yourself or leave it to some other (framework / utility) method.
If this is a one-off, the solution that you wish to avoid (using an "in-place" foreach loop to do the conversion) is a perfectly reasonable option. If you find yourself doing this quite often, you could extract that out into a generic utility method, as in cdhowie's answer.
If your restriction is only that you must target .NET 2.0 (but can use C# 3), consider LINQBridge, which is a reimplementation of LINQ to Objects for .NET 2.0. This will let you use the Cast sample you've provided without change. It will work on C# 2 too, but you won't get the benefits of the extension-method syntax, better type-inference etc.
If you don't care about performance, nor do you want to go to the trouble of writing a utility method, you could use the in-built ArrayList.ToArray method to help out, by creating an intermediary array that plays well with List<T> (this isn't all that shorter than a foreach):
ArrayList arrayList = ...
// Create intermediary array
BookingData[] array = (BookingData[]) arrayList.ToArray(typeof(BookingData));
// Create the List<T> using the constructor that takes an IEnumerable<T>
List<BookingData> list = new List<BookingData>(array);
Finally, I would suggest, if possible to abandon using the obsolete non-generic collection-classes altogether.
Let's keep it simple:
// untested
List<T> ConvertArrayList<T>(ArrayList data)
{
List<T> result = new List<T> (data.Count);
foreach (T item in data)
result.Add(item);
return result;
}
...
List<BookingData> newList = ConvertArrayList<BookingData>(oldList);
Use this method:
public static List<T> ConvertToList<T>(ArrayList list)
{
if (list == null)
throw new ArgumentNullException("list");
List<T> newList = new List<T>(list.Count);
foreach (object obj in list)
newList.Add((T)obj);
// If you really don't want to use foreach:
// for (int i = 0; i < list.Count; i++)
// newList.Add((T)list[i]);
return newList;
}
Then you can:
List<BookingData> someList = ConvertToList<BookingData>(someArrayList);
You have to use foreach:
foreach (Object item in list1)
{
list2.Add((BookingData)item);
}
ToList() method is nothing but the Synthetic sugar for creating a List representation but internally it is also using loop to generate the list item.
so it is much cleaner and simpler to use a foreach iterator block.

Apply function to all elements of collection through LINQ [duplicate]

This question already has answers here:
LINQ equivalent of foreach for IEnumerable<T>
(22 answers)
Closed 6 years ago.
I have recently started off with LINQ and its amazing. I was wondering if LINQ would allow me to apply a function - any function - to all the elements of a collection, without using foreach. Something like python lambda functions.
For example if I have a int list, Can I add a constant to every element using LINQ
If i have a DB table, can i set a field for all records using LINQ.
I am using C#
A common way to approach this is to add your own ForEach generic method on IEnumerable<T>. Here's the one we've got in MoreLINQ:
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
source.ThrowIfNull("source");
action.ThrowIfNull("action");
foreach (T element in source)
{
action(element);
}
}
(Where ThrowIfNull is an extension method on any reference type, which does the obvious thing.)
It'll be interesting to see if this is part of .NET 4.0. It goes against the functional style of LINQ, but there's no doubt that a lot of people find it useful.
Once you've got that, you can write things like:
people.Where(person => person.Age < 21)
.ForEach(person => person.EjectFromBar());
The idiomatic way to do this with LINQ is to process the collection and return a new collection mapped in the fashion you want. For example, to add a constant to every element, you'd want something like
var newNumbers = oldNumbers.Select(i => i + 8);
Doing this in a functional way instead of mutating the state of your existing collection frequently helps you separate distinct operations in a way that's both easier to read and easier for the compiler to reason about.
If you're in a situation where you actually want to apply an action to every element of a collection (an action with side effects that are unrelated to the actual contents of the collection) that's not really what LINQ is best suited for, although you could fake it with Select (or write your own IEnumerable extension method, as many people have.) It's probably best to stick with a foreach loop in that case.
You could also consider going parallel, especially if you don't care about the sequence and more about getting something done for each item:
SomeIEnumerable<T>.AsParallel().ForAll( Action<T> / Delegate / Lambda )
For example:
var numbers = new[] { 1, 2, 3, 4, 5 };
numbers.AsParallel().ForAll( Console.WriteLine );
HTH.
haha, man, I just asked this question a few hours ago (kind of)...try this:
example:
someIntList.ForEach(i=>i+5);
ForEach() is one of the built in .NET methods
This will modify the list, as opposed to returning a new one.
Or you can hack it up.
Items.All(p => { p.IsAwesome = true; return true; });
For collections that do not support ForEach you can use static ForEach method in Parallel class:
var options = new ParallelOptions() { MaxDegreeOfParallelism = 1 };
Parallel.ForEach(_your_collection_, options, x => x._Your_Method_());
You can try something like
var foo = (from fooItems in context.footable select fooItems.fooID + 1);
Returns a list of id's +1, you can do the same with using a function to whatever you have in the select clause.
Update: As suggested from Jon Skeet this is a better version of the snippet of code I just posted:
var foo = context.footable.Select(foo => foo.fooID + 1);
I found some way to perform in on dictionary contain my custom class methods
foreach (var item in this.Values.Where(p => p.IsActive == false))
item.Refresh();
Where 'this' derived from : Dictionary<string, MyCustomClass>
class MyCustomClass
{
public void Refresh(){}
}

Categories

Resources