Merge two (or more) lists into one, in C# .NET

You can use the LINQ Concat and ToList methods: Note that there are more efficient ways to do this – the above will basically loop through all the entries, creating a dynamically sized buffer. As you can predict the size to start with, you don’t need this dynamic sizing… so you could use: (AddRange is special-cased for ICollection<T> for efficiency.) I wouldn’t take this … Read more

How to convert code from C# to PHP

I know you’re hoping for someone who had experience but in case no one comes forward… You might consider just copy and pasting the code into a PHP script and checking what breaks. Write a parser to fix that, run it across the entire script, and see what’s the next thing that breaks. Continue until … Read more

How to easily initialize a list of Tuples?

c# 7.0 lets you do this: If you don’t need a List, but just an array, you can do: And if you don’t like “Item1” and “Item2”, you can do: or for an array: which lets you do: tupleList[0].Index and tupleList[0].Name Framework 4.6.2 and below You must install System.ValueTuple from the Nuget Package Manager. Framework 4.7 and above It is built … Read more

when do you need .ascx files and how would you use them?

It’s an extension for the User Controls you have in your project. A user control is a kind of composite control that works much like an ASP.NET Web page—you can add existing Web server controls and markup to a user control, and define properties and methods for the control. You can then embed them in ASP.NET Web … Read more

Sequence contains no matching element

Well, I’d expect it’s this line that’s throwing the exception: First() will throw an exception if it can’t find any matching elements. Given that you’re testing for null immediately afterwards, it sounds like you want FirstOrDefault(), which returns the default value for the element type (which is null for reference types) if no matching items are found: … Read more

Passing just a type as a parameter in C#

There are two common approaches. First, you can pass System.Type This would be called like: int val = (int)GetColumnValue(columnName, typeof(int)); The other option would be to use generics: This has the advantage of avoiding the boxing and pr