Skip to content

Instantly share code, notes, and snippets.

View frkn2076's full-sized avatar
🏠
Working from home

Furkan Öztürk frkn2076

🏠
Working from home
View GitHub Profile
@frkn2076
frkn2076 / MinimalAPI.cs
Last active October 19, 2021 04:49
.Net6-MinimalAPI
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Diagnostics;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<ApiContext>(opt => opt.UseInMemoryDatabase("SampleDB"));
var app = builder.Build();
app.Use(async (context, next) =>
public class ZipExample
{
public IEnumerable<(string, string)> Sample1()
{
var colors1 = new List<string>() { "Red", "Green", "Blue", "Black" };
var colors2 = new List<string>() { "Purple", "White", "Blue", "Red" };
var result = colors1.Zip(colors2);
// result = IEnumerable<(string,string)>() { ("Red", "Purple"), ("Green", "White"), ("Blue", "Blue"), ("Black", "Red") }
return result;
}
public class WhereExample
{
public IEnumerable<string> Sample1()
{
var colors = new List<string>() { "Red", "Green", "Blue", "Black" };
var result = colors.Where(x => x.Length > 4);
// result = IEnumerable<string> { "Green", "Black" }
return result;
}
public class UnionExample
{
public IEnumerable<string> Sample1()
{
var colors1 = new List<string>() { "Red", "Green", "Blue", "Black" };
var colors2 = new List<string>() { "Purple", "White", "blue", "Red" };
var result = colors1.Union(colors2);
// result = IEnumerable<string>() { "Red", "Green", "Blue", "Black", "Purple", "White", "blue" }
return result;
}
public class UnionByExample
{
public IEnumerable<string> Sample1()
{
var colors1 = new List<string>() { "Red", "Green", "Blue", "Black" };
var colors2 = new List<string>() { "Purple", "White", "blue", "Red" };
var result = colors1.UnionBy(colors2, x => x.Substring(0, 2));
// result = IEnumerable<string>() { "Red", "Green", "Blue", "Purple", "White", "blue" }
return result;
}
@frkn2076
frkn2076 / TryGetNonEnumeratedCountExample.cs
Created October 10, 2021 08:20
TryGetNonEnumeratedCount
public class TryGetNonEnumeratedCountExample
{
public (bool, int) Sample1()
{
var colors = new List<string>() { "Red", "Green", "Blue", "Black" };
var result = colors.TryGetNonEnumeratedCount(out int count);
// result = true (no iteration needed to find count)
// count = 4 (no iteration needed so returned count)
return (result, count);
}
public class ToLookupExample
{
public ILookup<bool, string> Sample1()
{
var colors = new List<string>() { "Red", "Green", "Blue", "Black" };
var result = colors.ToLookup(x => x.Length > 4);
// result = ILookup<bool, string> [ { Key: false, Items: [ "Red", "Blue" ] }, { Key: true, Items: [ "Green", "Black" ] } ]
return result;
}
public class ToListExample
{
public IEnumerable<string> Sample1()
{
IEnumerable<string> colors = new List<string>() { "Red", "Green", "Blue", "Black" };
var result = colors.ToList();
// result = List<string> { "Red", "Green", "Blue", "Black" }
return result;
}
}
public class ToHashSetExample
{
public HashSet<string> Sample1()
{
var colors = new List<string>() { "Red", "Green", "Blue", "Black", "red" };
var result = colors.ToHashSet();
// result = HashSet<string> { "Red", "Green", "Blue", "Black", "red" }
return result;
}
public class ToDictionaryExample
{
public Dictionary<string, string> Sample1()
{
var colors = new List<string>() { "Red", "Green", "Blue", "Black", "red" };
var result = colors.ToDictionary(x => x.Substring(0, 3));
// result = Dictionary<string, string> { { Key: "Red", Value: "Red" }, { Key: "Gre", Value: "Green" }, { Key: "Blu", Value: "Blue" }, { Key: "Bla", Value: "Black" }, { Key: "red", Value: "red" } }
return result;
}