How to Packaging files into one executable in C#

Published on

Use https://github.com/Fody/Costura in the past. Nuget install Install-Package Costura.Fody(https://www.nuget.org/packages/Costura.Fody/) Description about what it does from the source: Take all assemblies (and pdbs) that have been marked as “Copy Local” and embed them as resources in the target assembly.

How to convert mongo document into key value pair in .Net?

Published on

There is a ToDictionary method that can map this. var document = collection.Find(filter).Project(projection); var dictionary = documents.ToEnumerable().ToDictionary(key => key.client, value => value.url); A different approach var docs = new Dictionary<string, BsonDocument>(); var result = collection.Find(filter).Project(projection); result.ForEachAsync((bsonDoc) => { string name = bsonDoc.GetValue(“[Your Dictionary Key]”).AsString; if (!docs.ContainsKey(name)) { docs[name] = bsonDoc;}});

How to Get value from IEnumerable foreach loop in C#

Published on

JSON: { “AK”: { “Anchorage”: [{ “Name”: “John Doe”, “Address”: “123 Main St.”, “City”: “Anchorage”, “State”: “AK”, “Zip”: “12345”}], “Fairbanks”: [{ “Name”: “Sally Smith”, “Address”: “987 Main St.”, “City”: “Fairbanks”, “State”: “AK”, “Zip”: “98765”}]}} Uses Linq to JSON to select all the Location objects you want: var deserialisedJson = (IEnumerable)JsonConvert.DeserializeObject(json); JObject jObj = JObject.Parse(json); var … Continue reading How to Get value from IEnumerable foreach loop in C#

How to Create Groups from a list of Objects in C#

Published on

This will order your teams randomly and then put them in groups. The amount of output groups is according to the amount parameter: public static List<List<Team>> GenerateGroups(List<Team> teams, int amount) { return teams.OrderBy(item => Guid.NewGuid()) .Select((item, index) => new { Item = item, GroupIndex = index % amount }) .GroupBy(item => item.GroupIndex, (key, group) => … Continue reading How to Create Groups from a list of Objects in C#

How can I get data as JSON from Google API and store it in a variable

Published on

Use ReadAsStringAsync to get the json response… static void Main(string[] args) { HttpClient client = new HttpClient(); Task.Run(async () => { HttpResponseMessage response = await client.GetAsync(“https://maps.googleapis.com/maps/api/geocode/json?address=Los%20Angeles,CA=AIzaSyDABt”); string responseString = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseString); }); Console.ReadLine(); } If you use response.Content.ToString() it is actually converting the datatype of the Content to string so you will get System.Net.Http.StreamContent

difference between ToCharArray and ToArray in C#

Published on

The difference between ToCharArray and ToArray in C# ToCharArray method: ToCharArray method to extract the characters in a string to a character array. It then displays the original string and the elements in the array. using System; public class Example { public static void Main() { String s = “AaBbCcDd”; var chars = s.ToCharArray(); Console.WriteLine(“Original … Continue reading difference between ToCharArray and ToArray in C#

How to get all dates between two specific dates in C#

Published on

A shorter notation using Linq’s Range method uses the ability to already figure out the number of days using the TimeSpan.Days property after subtracting start from end. Assuming the start is before end you’d end up with: DateTime StartDate = new DateTime(1979, 10, 4); DateTime EndDate = new DateTime(2016, 10, 4); var dates = Enumerable.Range(0, … Continue reading How to get all dates between two specific dates in C#