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
81
votes
5answers
24k 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 ...
46
votes
2answers
248k views

Converting DataTable to List of class

I am using ExcelDataReader to import an Excel file to a dataset. Example Excel table: ...
40
votes
2answers
4k 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 ...
34
votes
3answers
16k views

Multiple indexes over an in memory collection for faster search

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

Are these if-statements too fancy?

I just rewrote this: if (budgetRemaining != 0 || totalOpenInvoices != 0) { } Like this: ...
28
votes
8answers
55k 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
4answers
98k views

Is the LINQ version faster than the foreach one?

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
5answers
16k 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
5answers
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
1answer
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
6answers
14k 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
7answers
14k 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
3answers
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
6answers
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
3answers
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
5answers
32k views

Finding elements inside ExpandoObject

Is it possible to simplify the code below? ...
14
votes
7answers
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
2answers
179k 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
9answers
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 ...
14
votes
2answers
9k 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: ...
13
votes
3answers
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
4answers
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
1answer
25k 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 ...
12
votes
4answers
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, ...
12
votes
2answers
1k 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
4answers
13k 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
2answers
25k 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
1answer
3k 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
4answers
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
6answers
6k 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
4answers
195k 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
1answer
10k 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 ...
11
votes
6answers
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 ...
11
votes
3answers
337 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
2answers
3k views

I had one job to do

The code I am working with: ...
11
votes
2answers
6k 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
3answers
838 views

Give me thy name, I'll invoke thee

This generic function invokes a property of a given object by name: ...
11
votes
2answers
103 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
2answers
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
1answer
525 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
4answers
5k 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
3answers
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
2answers
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
1answer
31k views

Update first and last item in List<T>

I am trying to do the following. I have a list of Custom objects: ...
10
votes
3answers
12k 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
2answers
611 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
3answers
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
1answer
196 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: ...
10
votes
1answer
223 views

Linq-to-Sage Implementation

Following-up on my last question where I wrapped the Sage API with a familiar IRepository interface, I decided to push the abstraction a step further, and... ...
10
votes
1answer
3k views

Multiple repository calls or LINQ query

These two methods do the exact same thing (or at least they are supposed to!). They both pass a simple test based on a mock context. Neither has been exposed to any integrated testing yet. Would ...

1
2 3 4 5
12