filtering a list using LINQ

EDIT: better yet, do it like that: var filteredProjects = projects.Where(p => filteredTags.All(tag => p.Tags.Contains(tag))); EDIT2: Honestly, I don’t know which one is better, so if performance is not critical, choose the one you think is more readable. If it is, you’ll have to benchmark it somehow. Probably Intersect is the way to go: void … Read more

Split an IEnumerable into fixed-sized chunks (return an IEnumerable where the inner sequences are of fixed length) [duplicate]

You could try to implement Batch method mentioned above on your own like this: static class MyLinqExtensions { public static IEnumerable<IEnumerable<T>> Batch<T>( this IEnumerable<T> source, int batchSize) { using (var enumerator = source.GetEnumerator()) while (enumerator.MoveNext()) yield return YieldBatchElements(enumerator, batchSize – 1); } private static IEnumerable<T> YieldBatchElements<T>( IEnumerator<T> source, int batchSize) { yield return source.Current; for … Read more

What should I use an IEnumerable or IList? [duplicate]

Generally speaking, you should try and use the least specific type that suits your purpose. IEnumerable is less specific than IList (IList implements IEnumerable) so unless you want something specific from IList (such as Count as you suggest, or perhaps Add, Delete, etc), I’d use IEnumerable. One benefit of remaining with IEnumerable is that you … Read more

Why does IEnumerable.ToList() return List instead of IList?

Returning List<T> has the advantage that those methods of List<T> that are not part of IList<T> are easily used. There are a lot of things you can do with a List<T> that you cannot do with a IList<T>. In contrast, Lookup<TKey, TElement> has only one available method that ILookup<TKey, TElement> does not have (ApplyResultSelector), and … Read more

Why use .AsEnumerable() rather than casting to IEnumerable?

Readability is the main issue here. Consider that Table.AsEnumerable().Where(somePredicate) is far more readable than ((IEnumerable<TableObject>)Table).Where(somePredicate). Or imagine wanting to execute part of the query on the SQL Server and the rest in memory: Table.Where(somePredicate) .Select(someProjection) .AsEnumerable() .SomethingElse() versus ((IEnumerable<SomeProjectionType>)Table.Where(somePredicate) .Select(someProjection)) .SomethingElse() Now, as for why such a method is useful at all think of the … Read more

what is IEnumerable in .net

It’s … something… that you can loop over. That might be a List or an Array or (almost) anything else that supports a foreach loop. It’s for when you want to be able to use an object with a foreach loop, but you don’t know exactly what type you’re dealing with, whether Array, List, or … Read more

What’s your favorite LINQ to Objects operator which is not built-in? [closed]

Append & Prepend (These have been added to .NET since this answer was written.) /// <summary>Adds a single element to the end of an IEnumerable.</summary> /// <typeparam name=”T”>Type of enumerable to return.</typeparam> /// <returns>IEnumerable containing all the input elements, followed by the /// specified additional element.</returns> public static IEnumerable<T> Append<T>(this IEnumerable<T> source, T element) { … Read more

Convert IEnumerable to DataTable

Look at this one: Convert List/IEnumerable to DataTable/DataView In my code I changed it into a extension method: public static DataTable ToDataTable<T>(this List<T> items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach(var prop in props) { tb.Columns.Add(prop.Name, prop.PropertyType); } foreach (var item in items) { var values = new object[props.Length]; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)