C#'s Linq is an simply amazing. However, as far as I know, there is no built-in ability to select multiple fields (of the same type) and return them, so here you go
C#'s Linq is an simply amazing. However, as far as I know, there is no built-in ability to select multiple fields (of the same type) and return them, so here you go
Google has developed a tool called PageSpeed. It highlighted couple of quick wins I could do to improve website's loading time. Website loading times are one of the most deceptive metrics to measure during development. Website development is frequently done on a local machine when download times are neligiable. It is only once the website
Recently, I was asked to write a 'real-time' calculator and the output value was to be stored in Redis. For legacy reasons, it also had to be stored in the database though, only the latest value had to be kept. As the calculator was spewing values, they were stored in a ConcurrentDictionary. When the timer
I came across this code when reading 'Concurrency in C# Cookbook' by Stephen Cleary. Did you know you can have an infinite Task.Delay without loops?
1 |
await Task.Delay(Timeout.InfiniteTimeSpan, ...) |
Obviously, there isn't much point in spinning a task that litterally will do nothing. You can use a CancellationToken to cancel the task.
1 2 |
using var cts = new CancellationTokenSource(); await Task.Delay(Timeout.InfiniteTimeSpan, cts.Token); |
No point. It's just one
Recently, I have been building a large database for the new House-Buddy project. The site uses a huge collection of data to identify the sweet-spots around a particular location. The site is hosted on a shared hosting which in theory had unlimited DB storage, but after they noticed the size of the database has grown
This is a ready made example of how to add a scheduled job to your ASP.NET core web application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class YourJob : IJob { public static async void Start() { IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); await scheduler.Start(); IJobDetail job = JobBuilder.Create<YourJob>().Build(); ITrigger trigger = TriggerBuilder.Create() .WithDailyTimeIntervalSchedule(s =>s.WithIntervalInSeconds(10)) .StartNow() .Build(); await scheduler.ScheduleJob(job, trigger); } Task IJob.Execute(IJobExecutionContext context) { return Task.Run(() => { Program.Counter++; }); } } |
In your Program.cs
1 2 3 4 5 6 7 8 9 |
public static int Counter = 0; public static void Main(string[] args) { var host = BuildWebHost(args); YourJob.Start(); host.Run(); } |
I hit an annoying issue today. I wanted to regain space after I dropped several columns in a table. When I run the famous query from StackOverflow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
SELECT t.NAME AS TableName, s.Name AS SchemaName, p.rows AS RowCounts, SUM(a.total_pages) * 8 AS TotalSpaceKB, CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00 / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB, SUM(a.used_pages) * 8 AS UsedSpaceKB, CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00/ 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB, (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB, CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB FROM sys.tables t INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE t.NAME NOT LIKE 'dt%' AND t.is_ms_shipped = 0 AND i.OBJECT_ID > 255 GROUP BY t.Name, s.Name, p.Rows ORDER BY t.Name |
I got a set of numbers which were the same before and after the removal has
1 |
DBCC CLEANTABLE('YOUR_DB', 'SCHEMA.TABLE'); |
There is, however, a potential issue with this approach if
Yesterday, when I was building a large DB from scratch I got an error saying:
1 |
Transaction Log for 'Database' is full due to LOG_BACKUP |
Running this simple query solved the issue:
1 |
ALTER DATABASE MyDB SET RECOVERY SIMPLE; |
I've been trying to apply a new migration and I was getting the following error: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Parent_dbo.Childs_ChildId". The conflict occurred in database "parents_db", table "dbo.Childs", column 'ChildId'. In my case, the table contained data. Therefore, either you have to assign a default value, set it as
Today, we encountered quite an interesting problem. MediaElement wasn't firing events. Using simple speech synthesis on Windows 8.1 using the speech synthesiser we tried:
1 2 3 4 5 6 7 8 9 |
using(SpeechSynthesizer synthesizer = new SpeechSynthesizer()) { SpeechSynthesisStream stream = await synthesizer.SynthesizeTextToStreamAsync("How are you feeling today?"); MediaElement mediaElement = new MediaElement(); mediaElement.IsMuted = false; mediaElement.SetSource(stream, stream.ContentType); mediaElement.MediaEnded += (a, b) => { System.Diagnostics.Debug.WriteLine("Steam has eneded."); }; mediaElement.Play(); } |
None of the variations worked. The speech worked just the event wasn't getting fired. After a bit of digging it turned out that in order to fire events MediaElement must