Questions tagged [linq]
Language INtegrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.
604
questions
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 ...
48
votes
2
answers
253k
views
Converting DataTable to List of class
I am using ExcelDataReader to import an Excel file to a dataset.
Example Excel table:
...
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 ...
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:
...
30
votes
7
answers
3k
views
Are these if-statements too fancy?
I just rewrote this:
if (budgetRemaining != 0 || totalOpenInvoices != 0)
{
}
Like this:
...
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:
...
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 ...
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 ...
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 ...
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:
...
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 ...
19
votes
7
answers
16k
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 ...
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 ...
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 ...
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 ...
15
votes
5
answers
43k
views
Finding elements inside ExpandoObject
Is it possible to simplify the code below?
...
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:
...
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 ...
14
votes
2
answers
188k
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 ...
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 ...
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 ...
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 ...
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 ...
13
votes
1
answer
38k
views
Update first and last item in List<T>
I am trying to do the following. I have a list of Custom objects:
...
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 ...
12
votes
4
answers
6k
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, ...
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:
...
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 ...
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 ...
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:
...
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 ...
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 ...
11
votes
6
answers
8k
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 ...
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 ...
11
votes
3
answers
387
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?
...
11
votes
2
answers
3k
views
I had one job to do
The code I am working with:
...
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 ...
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:
...
11
votes
2
answers
113
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 ...
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 ...
11
votes
1
answer
626
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 ...
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 ...
10
votes
4
answers
8k
views
Split IEnumerable by predicate
In order to divide an IEnumerable in C# by a predicate, I implemented the following (in a static class):
...
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 ...
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 ...
10
votes
1
answer
35k
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 ...
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 ...
10
votes
2
answers
629
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.
...
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:
...
10
votes
3
answers
6k
views
Filter out nulls from sequence of nullables to produce sequence of non-nullables
While introducing nullable reference types to our enterprise application's codebase, I found that we are often using LINQ on sequences of nullable reference types where we filter out the nulls using <...