When working with SQL Server, you may often need to count the number of unique values in a specific column. This is useful for analyzing data, detecting duplicates, and understanding dataset distributions.
To count the number of unique values in a column, SQL Server provides the COUNT(DISTINCT column_name) function. Here’s a simple example:
COUNT(DISTINCT column_name)
SELECT COUNT(DISTINCT column_name) AS distinct_count FROM table_name;
This query will return the number of unique values in column_name.
column_name
If you need to count distinct combinations of multiple columns, you can use a subquery:
SELECT COUNT(*) AS distinct_count FROM (SELECT DISTINCT column1, column2 FROM table_name) AS subquery;
This approach ensures that only unique pairs of column1 and column2 are counted.
column1
column2
By leveraging COUNT(DISTINCT column_name), you can efficiently analyze your database and extract meaningful insights. Happy querying!
When working with large files, reading the entire file at once may be inefficient or unnecessary, especially when you only need the first few lines.
In C#, you can easily read just the first N lines of a file, improving performance and resource management.
Reading only the first few lines of a file can be beneficial for:
Here's a simple and efficient method using C#:
using System; using System.IO; class FileReader { /// <summary> /// Reads the first N lines from a file. /// </summary> /// <param name="filePath">The path to the file.</param> /// <param name="numberOfLines">Number of lines to read.</param> /// <returns>Array of strings containing the lines read.</returns> public static string[] ReadFirstNLines(string filePath, int numberOfLines) { List<string> lines = new List<string>(); using (StreamReader reader = new StreamReader(filePath)) { string line; int counter = 0; // Read lines until the counter reaches numberOfLines or EOF while (counter < numberOfLines && (line = reader.ReadLine()) != null) { lines.Add(line); counter++; } } return lines.ToArray(); }
Here's a practical example demonstrating the usage of the method above:
string filePath = "C:\\largefile.txt"; int linesToRead = 10; string[] firstLines = FileReader.ReadFirstNLines(filePath, firstLinesCount); foreach (string line in firstLines) { Console.WriteLine(line); }
For a concise implementation, LINQ can also be used:
using System; using System.IO; using System.Linq; class FileReader { public static IEnumerable<string> ReadFirstNLines(string filePath, int numberOfLines) { // Take first N lines directly using LINQ return File.ReadLines(filePath).Take(numberOfLines); } }
string path = "C:\\largeFile.txt"; int n = 10; var lines = FileReader.ReadFirstNLines(path, n); foreach (string line in lines) { Console.WriteLine(line); }
File.ReadLines
File.ReadAllLines
ReadAllLines()
By limiting your reading operations to only the first few lines you actually need, you significantly enhance your application's efficiency and resource management.
When working with URLs in C#, encoding is essential to ensure that special characters (like spaces, ?, &, and =) don’t break the URL structure. The recommended way to encode a string for a URL is by using Uri.EscapeDataString(), which converts unsafe characters into their percent-encoded equivalents.
string rawText = "hello world!"; string encodedText = Uri.EscapeDataString(rawText); Console.WriteLine(encodedText); // Output: hello%20world%21
This method encodes spaces as %20, making it ideal for query parameters.
For ASP.NET applications, you can also use HttpUtility.UrlEncode() (from System.Web), which encodes spaces as +:
using System.Web; string encodedText = HttpUtility.UrlEncode("hello world!"); Console.WriteLine(encodedText); // Output: hello+world%21
For .NET Core and later, Uri.EscapeDataString() is the preferred choice.
Reading a file line by line is useful when handling large files without loading everything into memory at once.
✅ Best Practice: Use File.ReadLines() which is more memory efficient.
Example
foreach (string line in File.ReadLines("file.txt")) { Console.WriteLine(line); }
Why use ReadLines()?
Reads one line at a time, reducing overall memory usage. Ideal for large files (e.g., logs, CSVs).
Alternative: Use StreamReader (More Control)
For scenarios where you need custom processing while reading the contents of the file:
using (StreamReader reader = new StreamReader("file.txt")) { string? line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } }
Why use StreamReader?
Lets you handle exceptions, encoding, and buffering. Supports custom processing (e.g., search for a keyword while reading).
When to Use ReadAllLines()? If you need all lines at once, use:
string[] lines = File.ReadAllLines("file.txt");
Caution: Loads the entire file into memory—avoid for large files!
Register for my free weekly newsletter.