Posts

Showing posts with the label C#

Decimal number issue from server to client. Wow! Didn't expect this @_@

Image
Chapter 17   where a poor Decimal Number came from Server Side to JavaScript I worked on a page for days, adjusting the UI with CSS and HTML. Everything looked fine… until I started testing multi-lingual support. English — OK. Spanish — OK. Chinese — OK. Even right-to-left Hebrew (crooked and askew, but working). But Russian? Nope. In Russian the page stopped working completely: only static texts and images were visible. Nothing dynamic rendered. Of course it was a JavaScript error. Firefox DevTools showed this: That error led me to this JS code: I stared at those lines and saw nothing wrong — no missing variables, no typos, nothing obvious. I went to the original project code to find the exact non-rendered rows and noticed a server variable embedded into the client-side script: Which naturally led me to the server-side C# code responsible for generating that value: Nothing looked wrong there either. So I did what every developer does in ...

Remove Items From One List That Exist in Another List by ID in C#

Sometimes you need to remove elements from one collection if they already exist in another collection. The comparison is usually based on an identifier (ID). After testing several approaches, this turned out to be the cleanest and most readable solution using LINQ: lst1.RemoveAll(itemFromLst1 => lst2.Any(itemFromLst2 => itemFromLst2.ID == itemFromLst1.ID)); What This Code Does The logic can be read as: For each item in lst1 Check if any item in lst2 has the same ID If yes — remove it from lst1 In other words, this removes all elements from lst1 whose IDs already exist in lst2 . Why This Approach Works Well Concise and expressive No manual loops required No temporary collections needed Uses built-in List<T>.RemoveAll method The method RemoveAll modifies the list in place, which is often exactly what you want in filtering scenarios. Performance Consideration This solution uses Any() inside RemoveAll() , which means it performs a nest...

Pluralizing Words in C# (.NET) Using PluralizationService

Today I needed to generate a dynamic page title based on a category name. For example: My Playlists My Libraries My Categories The page receives a categoryID parameter, retrieves the corresponding category object, and then dynamically pluralizes the category name. Using PluralizationService in .NET .NET provides a built-in service for pluralization via System.Data.Entity.Design.PluralizationServices . using System.Data.Entity.Design.PluralizationServices; .... Category category = Category.GetByID(categoryID); PluralizationService ps = PluralizationService.CreateService( new System.Globalization.CultureInfo( "en-GB" )); Page.Title = $"My { ps.Pluralize( category .Name) }" ; ..... Important Note You must add a reference to System.Data.Entity.Design in your project. When to Use This This approach is useful when: Generating dynamic titles Building admin dashboards Creating REST endpoints with plural resource names Displaying catego...