Skip to content

Instantly share code, notes, and snippets.

View fredgdaley2's full-sized avatar

Fred Daley fredgdaley2

View GitHub Profile
@fredgdaley2
fredgdaley2 / FizzBuzz.cs
Created June 6, 2019 18:29
My FizzBuzz solution
using System;
public class Program
{
public static void Main()
{
bool divByThree = false;
bool divByFive = false;
for (var n = 0; n <= 100; n++)
{
@fredgdaley2
fredgdaley2 / ListToSelectList.cs
Created February 5, 2019 18:01
Create a selected item list from a anonymous list extension method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace Some.Extensions
{
public static class ListExtensions
{
public static IEnumerable<SelectListItem> ToSelectList<T>(
@fredgdaley2
fredgdaley2 / MvcBaseDataController.cs
Created February 5, 2019 17:29
Base data controller for MVC
public abstract class DataController : Controller
{
public DataController()
{
DbEfAccess = new YourEfDbEntitiesLib();
//#if DEBUG
// YourEfDbLib.Database.Log = s => Debug.Write(s);
//#endif
@fredgdaley2
fredgdaley2 / ReadLinesAsParallelFromZip.cs
Created August 8, 2018 17:23
Read file from Zip using AsParallel for fast file processing
public IEnumerable<string> ReadLines(Stream inputStream)
{
using (var reader = new StreamReader(inputStream, Encoding.UTF8))
{
while (!reader.EndOfStream)
{
yield return reader.ReadLine();
}
}
@fredgdaley2
fredgdaley2 / ReadFileAsParallel.cs
Created August 8, 2018 17:18
Read File using AsParallel for fast file processing
var processedLines = File.ReadLines(filePathToRead).AsParallel().AsOrdered().WithDegreeOfParallelism()
Select(l => GetProcessedLine(l));
string outputFilename = @"output.csv";
using (var output = new StreamWriter(outputFilename))
{
output.AutoFlush = true;
foreach (var processedLine in processedLines)
{
@fredgdaley2
fredgdaley2 / FieldMap.cs
Created August 8, 2018 17:14
Adds fields to map object. Used for batch file APIs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class FieldMap
{
public Dictionary<string,string> Items { get; set; }
@fredgdaley2
fredgdaley2 / FileIterator.cs
Created August 8, 2018 17:10
Iterate through file.
using System;
using System.Linq;
using System.IO;
public class FileIterator : IDisposable
{
StreamReader reader;
public FileIterator(string path)
@fredgdaley2
fredgdaley2 / FixedWidthLineParser.cs
Created August 8, 2018 17:09
Fixed-Width Line Parser
using System;
using System.Collections.Generic;
using System.Linq;
public class FixedWidthLineParser
{
public string[] Parse(string lineToParse, int[] widths, bool trimWhiteSpace = false, int maxFields = -1)
{
char[] characters = lineToParse.ToCharArray();
@fredgdaley2
fredgdaley2 / DelimitedLineParser.cs
Last active August 8, 2018 17:07
Delimited line parser. RFC 4180 Compliant
using System;
using System.Collections.Generic;
using System.Linq;
public class DelimitedLineParserResult
{
public string[] Values { get; set; }
public bool Errored { get; set; }
public string ErrorMessage { get; set; }
}
@fredgdaley2
fredgdaley2 / JsonToClass.cs
Created June 18, 2018 13:19
Deserialize Json to a Class
using Newtonsoft.Json;
public static T ToClass<T>(string data, JsonSerializerSettings jsonSettings = null)
{
var response = default(T);
if (!string.IsNullOrEmpty(data))
response = jsonSettings == null
? JsonConvert.DeserializeObject<T>(data)
: JsonConvert.DeserializeObject<T>(data, jsonSettings);