Snippets Collections
public static class PHVExtensions
{
    public static IEnumerable<T> SetValue<T>(this IEnumerable<T> items, Action<T>
         updateMethod)
    {
        foreach (T item in items)
        {
            updateMethod(item);
        }
        return items;
    }
}

/*
With that method in place, I can write my statement like this:
customers.Where(c => c.IsValid).SetValue(c => c.CreditLimit = 1000).ToList();

Or like this:
var newCustomers = customers.Where(c => c.IsValid).SetValue(c => c.CreditLimit = 1000);

Or like this for Entity Framework:
db.Customers.Where(c => c.IsValid).ToList().SetValue(c => c.CreditLimit = 1000);
*/
Dim resultSite As String =
    appEnums.OrderBy(Function(kvp) kvp.Key)
        .SkipWhile(Function(kvp) kvp.Key <= startSite)
        .Where(Function(kvp) kvp.Key <> mainSite AndAlso kvp.Key <> returnSite)
        .Select(Function(kvp) kvp.Value).FirstOrDefault()
//Applies an accumulator function over a sequence.

//public static TSource Aggregate<TSource>
//(this System.Collections.Generic.IEnumerable<TSource> source,
//Func<TSource,TSource,TSource> func);

string sentence = "the quick brown fox jumps over the lazy dog";

// Split the string into individual words.
string[] words = sentence.Split(' ');

// Prepend each word to the beginning of the
// new sentence to reverse the word order.
string reversed = words.Aggregate((workingSentence, next) =>
                                      next + " " + workingSentence);

Console.WriteLine(reversed);

// This code produces the following output:
//
// dog lazy the over jumps fox brown quick the




//Applies an accumulator function over a sequence. The specified seed value is used as the //initial accumulator value.

//public static TAccumulate Aggregate<TSource,TAccumulate> (this //System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, //Func<TAccumulate,TSource,TAccumulate> func);


int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };

// Count the even numbers in the array, using a seed value of 0.
int numEven = ints.Aggregate(0, (total, next) =>
                                    next % 2 == 0 ? total + 1 : total);

Console.WriteLine("The number of even integers is: {0}", numEven);

// This code produces the following output:
//
// The number of even integers is: 6





//Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

//public static TResult Aggregate<TSource,TAccumulate,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func, Func<TAccumulate,TResult> resultSelector);

string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" };

// Determine whether any string in the array is longer than "banana".
string longestName =
    fruits.Aggregate("banana",
                    (longest, next) =>
                        next.Length > longest.Length ? next : longest,
    // Return the final result as an upper case string.
                    fruit => fruit.ToUpper());

Console.WriteLine(
    "The fruit with the longest name is {0}.",
    longestName);

// This code produces the following output:
//
// The fruit with the longest name is PASSIONFRUIT.
class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
    public long TrackingNumber { get; set; }
}

public static void ILookupExample()
{
    // Create a list of Packages to put into an ILookup data structure.
    List<Package> packages = new List<Package>
		{
      		new Package { Company = "Coho Vineyard", Weight = 25.2, TrackingNumber = 89453312L },
      		new Package { Company = "Lucerne Publishing", Weight = 18.7, TrackingNumber = 89112755L },
      		new Package { Company = "Wingtip Toys", Weight = 6.0, TrackingNumber = 299456122L },
      		new Package { Company = "Contoso Pharmaceuticals", Weight = 9.3, TrackingNumber = 670053128L },
        	new Package { Company = "Wide World Importers", Weight = 33.8, TrackingNumber = 4665518773L }
		};

    // Create a Lookup to organize the packages. Use the first character of Company as the key value.
    // Select Company appended to TrackingNumber for each element value in the ILookup object.
    ILookup<char, string> packageLookup = packages.ToLookup(
        p => p.Company[0],
        p => p.Company + " " + p.TrackingNumber
        );

    // Iterate through each value in the ILookup and output the contents.
    foreach (var packageGroup in packageLookup)
    {
        // Print the key value.
        Console.WriteLine(packageGroup.Key);
        // Iterate through each value in the collection.
        foreach (string str in packageGroup)
            Console.WriteLine("    {0}", str);
    }

    // This code produces the following output:
    //
    // C
    //     Coho Vineyard 89453312
    //     Contoso Pharmaceuticals 670053128
    // L
    //     Lucerne Publishing 89112755
    // W
    //     Wingtip Toys 299456122
    //     Wide World Importers 4665518773
}
star

Mon Jul 04 2022 19:42:53 GMT+0000 (Coordinated Universal Time) https://visualstudiomagazine.com/articles/2019/07/01/updating-linq.aspx

#c# #linq #lambda #extensionmethod
star

Wed Jun 29 2022 18:02:56 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/10727622/vb-net-and-linq-query-on-a-dictionary

#vb.net #linq #dictionary
star

Mon Apr 11 2022 16:59:32 GMT+0000 (Coordinated Universal Time) https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate?view=netframework-4.8#system-linq-enumerable-aggregate-3(system-collections-generic-ienumerable((-0))-1-system-func((-1-0-1))-system-func((-1-2)))

#linq #aggregate #seed #reverse #string
star

Mon Apr 11 2022 14:46:56 GMT+0000 (Coordinated Universal Time) https://docs.microsoft.com/en-us/dotnet/api/system.linq.ilookup-2?view=net-6.0

#ilookup #lookup #lambdaexpression #linq

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension