Linq OrderBy against specific values

If you put your preferences into a list, it might become easier. List<String> data = new List<String> { “A”,”B”,”A”,”C”,”B”,”C”,”D”,”E” }; List<String> preferences = new List<String> { “A”,”B”,”C” }; IEnumerable<String> orderedData = data.OrderBy( item => preferences.IndexOf(item)); This will put all items not appearing in preferences in front because IndexOf() returns -1. An ad hoc work around … Read more

LINQ identity function

Unless I misunderstand the question, the following seems to work fine for me in C# 4: public static class Defines { public static T Identity<T>(T pValue) { return pValue; } … You can then do the following in your example: var result = enumerableOfEnumerables .SelectMany(Defines.Identity); As well as use Defines.Identity anywhere you would use a … Read more

Using LINQ, select list of objects inside another list of objects

You’re trying to select multiple result objects for each ClassB object in the original list. Therefore, you’re looking for the SelectMany extension method: var results = classBList.SelectMany(b => b.MyObjects).Distinct(); If you want to use query expressions, you’ll need to use two from clauses: var results = (from b in classBList from a in b.MyObjects select … Read more

How does LINQPad reference other classes, e.g. Books in the LINQ in Action samples

If you right click in the code editor in LINQPad and choose Advanced Query Properties, there are two dialogs: Additional References and Additional Namespace Imports. 1) In Additional References, choose Add then click Browse and navigate to your custom assembly. 2) Then, in Additional Namespace Imports, type the namespaces you want to import from that … 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

Linq – SelectMany Confusion

Here is your query using SelectMany, modeled exactly after your example. Same output! var customerOrders2 = customers.SelectMany( c => orders.Where(o => o.CustomerId == c.Id), (c, o) => new { CustomerId = c.Id, OrderDescription = o.Description }); The first argument maps each customer to a collection of orders (completely analagous to the ‘where’ clause you already … Read more

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