Questions tagged [linq]

Language INtegrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.

Filter by
Sorted by
Tagged with
86 votes
5 answers
25k views

SudokuSharp Solver with advanced features

Even though it's the first time I'm writing something this "big", it feels like I know C# quite well (it is very similar to Java after all). It's been nice to learn LINQ also and I am very impressed ...
Simon Forsberg's user avatar
48 votes
2 answers
252k views

Converting DataTable to List of class

I am using ExcelDataReader to import an Excel file to a dataset. Example Excel table: ...
devspider's user avatar
  • 641
40 votes
2 answers
5k views

Wait, is this... LINQ?

Context I'm working on a little project that consists in a series of Microsoft Excel add-ins (.xlam). The code being submitted for review here, is located in the ...
Mathieu Guindon's user avatar
35 votes
3 answers
17k views

Multiple indexes over an in memory collection for faster search

I have a big in-memory collection of following simplified class: ...
Atashbahar's user avatar
30 votes
7 answers
3k views

Are these if-statements too fancy?

I just rewrote this: if (budgetRemaining != 0 || totalOpenInvoices != 0) { } Like this: ...
Aaron Anodide's user avatar
28 votes
8 answers
59k views

LINQ approach to flatten Dictionary to string

I have a Dictionary<string,string> and want to flatten it out with this pattern: ...
Martin's user avatar
  • 383
27 votes
4 answers
102k views

Filtering a list of vertices that lie inside a cylinder, with and without LINQ

Since I don't know how LINQ works under the hood, I can't decide what version is best to use in term of rapidity of execution. I've done some testing with my testing data (Point Cloud) but I can't see ...
Jean-François Côté's user avatar
24 votes
5 answers
17k views

Single line FizzBuzz solution in LINQ

Now FizzBuzz itself isn't a big challenge but I agree that it can be a good tool to see if someone can code or not. I wanted to practice my LINQ a little bit so here's my single line FizzBuzz ...
Overly Excessive's user avatar
23 votes
5 answers
4k views

Linq me a FizzBuzz

I got this requirement recently: Write some code that prints out the following for a contiguous range of numbers: the number 'fizz' for numbers that are multiples of 3 'buzz' for numbers that are ...
user2858451's user avatar
20 votes
1 answer
19k views

LINQ to SQL Joining Entities

I have two tables, one for jobs, and one for the names of industries (e.g. automotive, IT, etc). In SQL I would just do: ...
George's user avatar
  • 362
19 votes
6 answers
15k views

Increment up the build number in AssemblyInfo.cs on every build

I just wrote this short little program to increment up the build number for my projects every time I build them. After compiling this exe, I just call it in the pre-build command line. I did take ...
BenVlodgi's user avatar
  • 4,278
19 votes
7 answers
15k views

Optimizing List<string> performance

I have a List<string> being stored in my cache with about 600K members. I want this to act as the backend for an Ajax autocomplete box. It's accessible ...
Paul's user avatar
  • 293
19 votes
3 answers
1k views

Using C# properties for simple operations, where should I draw the line?

Should I be putting Linq statements inside of an objects properties? Is that a best practice or is that a no no? Also if that is ok, where do I draw the line with this? I assume db access is ...
DeadlyChambers's user avatar
18 votes
6 answers
6k views

Solving Fizz Buzz using LINQ in C#

The Fizz Buzz challenge is well-known and should not need any explanations, right? I recently got a bit bored and came up with a solution for Fizz Buzz using LINQ and even asynchronous code and a lot ...
Wim ten Brink's user avatar
17 votes
3 answers
2k views

ExactlyOne extension method

I often find in codebases something on the order of if (sprockets.Count() > 0) which is easily replaced with LINQ's ...
Jesse C. Slicer's user avatar
15 votes
5 answers
42k views

Finding elements inside ExpandoObject

Is it possible to simplify the code below? ...
user2926340's user avatar
15 votes
2 answers
10k views

Generic Calculator and Generic Number

.NET does not support generic numbers. It is not possible to enforce a generic method with generic argument T that T is a number. The following code will simply not compile: ...
Martin Mulder's user avatar
14 votes
7 answers
4k views

Imposing a sort order on a query

I've always been told "never use goto, there's always a better way" and for the longest time I just accepted it. Lately though, I've been running into such scenarios in which I have to repeat this bit ...
Shelby115's user avatar
  • 1,961
14 votes
2 answers
187k views

LINQ query that filters elements from a list of object

I have two lists of objects Person and PersonResult. Both are linked through the property ...
WizLiz's user avatar
  • 293
14 votes
9 answers
1k views

How can I refactor to avoid repetition finding the index of strings in a list?

I have a list of strings where I want to find the index of a first- and second-choice video compressor. Logic: If the first choice is available, return its index. If not, and the second choice is ...
JYelton's user avatar
  • 307
13 votes
3 answers
5k views

What do you think of this improvement of Linq's GroupBy method?

Linq already has a set of GroupBy methods for enumerable, which returns IEnumerable<IGrouping<TKey, TSource>>. I dislike this return type because it ...
Louis Rhys's user avatar
13 votes
4 answers
1k views

Counting the number of points that have both a larger theta and a larger radius than a given point

I don't like the fact that I am selecting all the points, even though I only care about the count of them. Which of the from parts should appear first, if it even ...
mjcopple's user avatar
  • 873
13 votes
1 answer
27k views

Filtering a collection by an async result

I'm trying to do something like this: var loudDogs = dogs.Where(async d => await d.IsYappyAsync); The "IsYappyAsync" property would return a ...
mbursill's user avatar
  • 233
13 votes
1 answer
37k views

Update first and last item in List<T>

I am trying to do the following. I have a list of Custom objects: ...
Donniewiko's user avatar
12 votes
1 answer
12k views

Find the odd occurrences in an array

Recently, I came across this problem on Codility. I am aware there is a bit of similarity to this question and this question, but I am trying to achieve this in C#. The problem is as follows: A ...
Tolani's user avatar
  • 2,465
12 votes
4 answers
5k views

Calculating totals of a transaction

I am working on a project where I have to calculate the totals of a transaction. Unfortunately, coupons have proven to be quite an issue. There are four types of coupons: transaction percentage, ...
user avatar
12 votes
2 answers
2k views

Given an array A[] and a number x, check for pair in A[] with sum as x

LINQ/FP style: Assert.IsTrue(new[] {1,2,3}.AnyPairSum(4)); Where: ...
Dmitry Nogin's user avatar
  • 6,131
12 votes
4 answers
15k views

Generate sequence in Linq

I want to generate a sequence using Linq that goes from 10 to 100 with a step size of 10. The sequence also must contain a custom value (in the correct order). This is what I have now, and I'm ...
Magnus's user avatar
  • 375
12 votes
2 answers
28k views

CSV reader using StreamReader and LINQ

Mostly due to readability, I am using the following code to find a specific csv file on a drive read that CSV file using a streamReader parse it into a ...
Aeolus's user avatar
  • 165
12 votes
2 answers
7k views

Query Builder pattern with use of FluentApi

Recently, I've come up with an idea of building a custom query builder combine with FluentApi. Expected usage: ...
Kien Chu's user avatar
  • 231
12 votes
1 answer
5k views

Linq for ObservableCollection

Purpose ObservableCollection<T> is not strictly a WPF class, but its intended purpose seems to be for use in WPF. It's the standard implementation of ...
Peter Taylor's user avatar
  • 24.1k
11 votes
4 answers
2k views

Country code lookup for each line in a CSV file

I'm trying to write a program that has a for loop, inside of which I have lambda expression to get a certain value. The loop has about 20,000 iterations. Including ...
psyoptica's user avatar
  • 121
11 votes
6 answers
7k views

PermMissingElem- find the missing element in a given permutation in C#

I just encountered this question in codility but apparently this question has been asked in java already (here, here). Task description A zero-indexed array A consisting of \$N\$ different integers ...
Tolani's user avatar
  • 2,465
11 votes
4 answers
200k views

Using LINQ or Lambda instead of nested and multiple foreach statements

I want to use a better syntax than nested foreach statements to overwrite the initial list with items from the second list. In the code below: I want to overwrite ...
ken1nil's user avatar
  • 221
11 votes
3 answers
386 views

FizzBuzz in accordance with standards

I wrote FizzBuzz. My aim is to use C# Code Standards correctly, and write flexible code. How can I improve my solution according to code standards? How can it be better in general? ...
Artxzta's user avatar
  • 111
11 votes
2 answers
3k views

I had one job to do

The code I am working with: ...
user avatar
11 votes
2 answers
7k views

String join of distinct string

I have a class like this one: class EmailClass { public string MailAdresse { get; set; } public string MailAdresseCC { get; set; } } Through a JSON ...
Abaco's user avatar
  • 255
11 votes
3 answers
845 views

Give me thy name, I'll invoke thee

This generic function invokes a property of a given object by name: ...
user avatar
11 votes
2 answers
109 views

IdentifierReferenceResolver: resolving at project level

One of the most important parts at the core of rubberduck is the IdentifierReferenceResolver class, which is responsible for resolving each declaration as the parse ...
Mathieu Guindon's user avatar
11 votes
2 answers
3k views

Poker Hand Evaluator, take 2

This is following up on my previous attempt, which was admittedly done fast and not-so-well. This code attempts to allow comparing two poker hands to determine a winner, not only evaluating a given ...
Mathieu Guindon's user avatar
11 votes
1 answer
614 views

C++ linq-like library

I want to implement something like C# Linq in C++ Currently only select and where is implemented, but others should be fairly ...
Bryan Chen's user avatar
10 votes
6 answers
3k views

Too Many Staff Meetings

I recently got in really deep on a question to find the overlapping meetings and I rewrote a bit of the code and added some things to the code, and I would just like a general review of my code and ...
Malachi's user avatar
  • 28.6k
10 votes
4 answers
7k views

Split IEnumerable by predicate

In order to divide an IEnumerable in C# by a predicate, I implemented the following (in a static class): ...
Heman Gandhi's user avatar
10 votes
3 answers
2k views

Refactor foreach statement to LINQ

I have been working on a project and I'm actually refactoring some code. I have encountered myself with lots of foreach and if statements, which could be easily replace with LINQ. But I have this ...
Sweeden's user avatar
  • 149
10 votes
2 answers
2k views

Get result from UI thread to non-UI thread when using await Task.Run

I have an application where I use Task to run an operation. During the operation, there can be a problem which will need some user interaction (continue or not). I ...
hs2d's user avatar
  • 101
10 votes
1 answer
34k views

Using Linq instead of multiple foreach loops

A project I'm working on often has lists within lists within lists etc. The original author often uses complicated linq expressions, but when adapting them I mostly get hopelessly bogged down and ...
BMills's user avatar
  • 231
10 votes
3 answers
13k views

Read/write to XML using LINQ

This will compile, but I'm thinking that this implementation of read/write is very hard coded and would not be easily adapted to larger situations where large XML documents are being processed or if ...
Viperfourfive's user avatar
10 votes
2 answers
628 views

Left join query of user profiles

I have the following left join LINQ query that is returning the results that I expect, but it does not "feel" right. I need ALL records from the UserProfile table. ...
Schenz's user avatar
  • 203
10 votes
3 answers
2k views

Tracking Entity Changes (not EF)

So, I kept refactoring my Sage300 API wrapper - I wanted the client code to feel just like using Entity Framework - this is the closest I got to it: ...
Mathieu Guindon's user avatar
10 votes
1 answer
215 views

Refactor VB.NET to C#.Net using Linq

I am rewriting a VB.NET application in C#. I will not subject you to the original code because it's pretty messy. Below is the converted C# code: ...
Robert's user avatar
  • 583

1
2 3 4 5
13