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
0 votes
1 answer
2k views

adding properties together from list of objects using LINQ

Looking to see if there is a simpler way to achieve this. I would love if I could keep the return as a one-line LINQ expression. I want to grab the total number of ...
Premier Bromanov's user avatar
2 votes
0 answers
44 views

Correct Way to Retrieve Header Records With Filtered Detail Records [closed]

I wrote this in LinqPad. It works. The whole point was to retrieve header records, and only include some detail records. I'm looking for possible improvements to this, specifically around the ...
Bob Horn's user avatar
  • 203
2 votes
2 answers
3k views

Linq IEnumerable extensions for void functions

I found myself wanting to use Linq to map void methods to an IEnumerable to modify all items. The existing Linq methods require a return variable when mapping, since they are based on ...
JAD's user avatar
  • 2,909
2 votes
0 answers
125 views

Implementing the IQueryable counterpart method of the IEnumerable method?

I have created this method to simulate SQL's IN operator: ...
mshwf's user avatar
  • 121
6 votes
3 answers
12k views

LINQ method that selects an item based on previous one

my requirement is to implement a way to filter a collection of elements based on a value of previous one. For example to only select a number that is larger than the previous one. I don't currently ...
LadislavBohm's user avatar
5 votes
2 answers
243 views

Porting functional programming code to Linq

I am very new to C# programming, so for practice I thought I would try to port a simple haskell function over to Linq. From learn you a haskell, there is the following implementation of powerset: <...
YonedaIsLove's user avatar
5 votes
1 answer
88 views

Representing relations between devices when presented with only one-to-many mappings

There are devices that communicate between each other. Already existing in the Legacy codebase is a list of one-to-many mappings between the devices. I have written a function in my backend that ...
ValarMorghulis's user avatar
0 votes
1 answer
67 views

Data validation for to check required fields are mapped

Can I break the below foreach and IF statements into LINQ expression. Also, performance wise which will be better. Please suggest. ...
user2781550's user avatar
6 votes
1 answer
79 views

Return true from enumerable that calls function unless that function ever returns false

Is there a tighter way to write this code? ...
user875234's user avatar
0 votes
1 answer
909 views

Entity Framework MVC updating db using a repository method [closed]

I have inherited this code from a legacy project, and it is throwing a New transaction is not allowed because there are other threads running in the session. error. Now I know the problem here is ...
kolin's user avatar
  • 129
1 vote
1 answer
226 views

Search through multiple lists, but have functions that do that in parts

I have an Entity Framework table of Addresses. I'm building a Repository that would allow me to search for an Address by either parts of the Address or the whole address. Here's what I've got so far: ...
Ryan Taite's user avatar
0 votes
2 answers
2k views

Importing complex XML file into a flat object

I am trying to parse xml file using XDocument, basically, I have to select xml element based on requirement and convert into list model. Below is sample code I am ...
M005's user avatar
  • 131
1 vote
4 answers
6k views

Chain LINQ expressions for filtering

Example of my method where I am chaining linq expressions based on model values provided: ...
sensei's user avatar
  • 286
4 votes
2 answers
640 views

Accessing fields in LINQ query for customers with overdue balances

Practicing my autodidactism using a free Kindle Unlimited book to get myself acquainted with the basics of C# in hopes of getting enough of a foundation to get into automation and I still feel a ...
Graham Parker's user avatar
0 votes
1 answer
120 views

Add data into lists at runtime, from a database

Presentation: I am trying to write classes to store data from a database. The data consists in objects, let's call them "Areas". Each area has a name and possibly 'child' areas: ...
Maxime's user avatar
  • 351
4 votes
2 answers
23k views

Creating a list with nested lists

I have made algorithms to create a list with nested lists in order to then show them in a tree view. From what users told me, my view method is fine, but the way I create the final list is probably ...
Flexabust Bergson's user avatar
1 vote
2 answers
8k views

Split a list of objects into a list and a grouping, based on criteria

I'd like to know what's the most efficient and correct way to do the following: We're given a list of objects. Here's an exampler object: ...
HelterSkelter's user avatar
6 votes
2 answers
16k views

Get total count of object's elements and all children objects` elements

I have a table, Taxonomy, with data that looks something like the following: ...
Tot Zam's user avatar
  • 165
5 votes
2 answers
4k views

Efficiency of a linq query on a nested structure

For a survey website I've written this query to show the survey results, with a percentage for each chosen answer: ...
Arnold S's user avatar
1 vote
1 answer
85 views

Aggregating clients and client types

The code I have put together achieves what I want, but I don't know if it is the right way to go about tackling the problem. I am dealing with 2 tables (CLIENT, CLIENTTYPE) each of which I have an ...
Free2Rhyme2k's user avatar
8 votes
1 answer
3k views

GroupBy(params string[] fields)

Linq to Object GroupBy extension method allows grouping by projection: ...
Dmitry Nogin's user avatar
  • 6,123
4 votes
4 answers
1k views

Selecting names with two consecutive, same letters

I've a list of names that I'd like to iterate over and extract from it those names that have two letters that are the same right next to each other (i.e. Bill, Terry, Debby, Aaron, etc.). The below ...
Delfino's user avatar
  • 509
9 votes
3 answers
733 views

LINQ-ifying Colour Generation from Strings

Recently I was thinking about how Google, Microsoft Outlook on Windows Phone (and now many others) method of giving you a coloured icon with a letter in the centre of it works. Essentially, given ...
Der Kommissar's user avatar
-3 votes
2 answers
100 views

Classes for plotting data in daily, monthly, and yearly groups [closed]

I have the following code, Only difference is way it groups data, Is there any way to make this into one logic by passing group by arguments, So the duplicate code can be removed. ...
user145394's user avatar
2 votes
2 answers
1k views

Sort a list for a GridView

I've written the following piece of code to sort my list, which is the data source to my GridView, depending on the column to be sorted on. It seems to me that there is quite a bit of redundancy, and ...
Fahad's user avatar
  • 253
1 vote
1 answer
1k views

Counting matching rows in IQueryable

I'm designing a basic ticketing system using ASP.NET MVC with Entity Framework. I'm passing a DbSet<TicketModel> into a method as an ...
Sinjai's user avatar
  • 227
1 vote
3 answers
595 views

Get a collection of child positions for a given parent position

I am trying to get a complete tree in a single loop using an iterative function. For instance, this is what I have: ...
vikramjb's user avatar
  • 121
7 votes
2 answers
17k views

Comparing two lists of class objects similar to a Diff Tool

My requirement is to write a program that sort of mimics diff tools. Yes there are quite a few libraries and open source code that accomplishes this purpose, but I would like to write my own comparer. ...
Sach's user avatar
  • 185
1 vote
3 answers
7k views

Combine a boolean condition with a Where clause in same query

I'm trying to see if I can somehow reduce the length of this query. I have a common part with the string.Compare(...), is IsProperty == true I would like to take all strings where Value is property, ...
Ziv Weissman's user avatar
2 votes
1 answer
128 views

Group a list of products and services

I'm working with a third part service of my client that is providing me a list of products and services, which is a little bit of a mess. The list will return all of the ...
Ariel's user avatar
  • 145
8 votes
4 answers
15k views

Filter data with optional parameters

I would make a filter to filter my data using next parameters: First name Last name Language Profile A minimum date of registration A maximum date of registration Success All filter options are not ...
H. Pauwelyn's user avatar
-2 votes
1 answer
101 views

Addressing "System.NullReference" Resharper warning in function [closed]

TLDR: How can I check "DeclaringType" for null without causing a further cascade of problems in the function? I have a C# LINQ query that uses reflection to list all Areas, Controllers, and Actions ...
RandomHandle's user avatar
6 votes
3 answers
809 views

The Atbash Cipher

I am doing some exercises in C# and I have just finished writing my own implementation of the atbash cipher, here is the code: ...
Sylvoo's user avatar
  • 161
3 votes
1 answer
280 views

Converting a ResourceSet of bitmaps into a sorted List<float>

I have a map. It can be zoomed-in and out. At first, I used one large image. Scrolling the mouse wheel defined the desired zoom level. Every scrolling created a new instance of the image in the ...
Michael Haddad's user avatar
5 votes
1 answer
8k views

Chaining multiple predicates

I have a some filters that chains around 30 Expression<Func<T, bool>> using linq to entities. Currently this is how ...
Train's user avatar
  • 301
0 votes
1 answer
5k views

A complex selection LINQ query

I am stuck on recursive selection of the objects for TreeView. I have some class IMenuDataSet, that stores objects, named 'I' in the List property: ...
Arthur's user avatar
  • 111
2 votes
1 answer
183 views

Extension method replacing elements from an IEnumerable<T> collection follow up

I got some amazing feedback on my previous question and I decided to extend this method and add it to my own library where every now and than I will add some methods I feel can be useful in the future....
Denis's user avatar
  • 8,448
9 votes
1 answer
3k views

Delete duplicate files

I have this function which looks through ALL files found in a directory (including sub folders) and deleted the duplicated file. ...
user3159792's user avatar
3 votes
1 answer
61 views

Loading and parsing WinForms menus using LINQ

I'm refactoring a WinForms utility program for building and testing regular expressions. The program has a menu item which lists the RegexOptions enumeration, ...
Memetican's user avatar
  • 131
6 votes
1 answer
169 views

Returning BindingList<T>

I came to work at a place not really understanding data binding. I have been working with it for about a year now, and I'd like to get some clarification on what is going on. Particularly, I'd like ...
jp2code's user avatar
  • 286
3 votes
1 answer
2k views

Beginner's LINQ exercise to print the odd numbers in descending order

I'm going through C# Operators which led me to more specifically Lambda Expressions (C# Programming Guide). I've modified one of the examples from just getting the ...
IvenBach's user avatar
  • 3,487
4 votes
2 answers
13k views

Mapping .csv rows to a Dictionary

I have a .csv file in which each row represents an association between a software application and a web server. My goal is to fetch a List<Server> by ...
Tyler Daniels's user avatar
7 votes
3 answers
24k views

Joining to in memory List

I have an in memory list that I am joining to results from a query using entity framework. My list will most likely never be greater than 2500 records. The results from the database can fluctuate, ...
DrivenTooFar's user avatar
5 votes
1 answer
15k views

Calculate working days and excluding specific dates (Holidays)

I read some questions/answers related to functions in C# to calculate the working days; some use an extended code to achieve that. I have a data table with more than 50,000 rows and I required an ...
MiBol's user avatar
  • 237
3 votes
2 answers
336 views

linq.Any() check on large database

Is there a way to speed up the Linq.Any check or perform the check in another way to enhance performance. I have a mastercodes table that records unique codes for a campaign. The code below will ...
Andrew's user avatar
  • 133
4 votes
1 answer
117 views

Reduce namespaces

I have a collection of full type names where I need to reduce common namespaces. For this purpose I wrote two extensions. The first one explodes each namespace into pieces and the second one removes ...
t3chb0t's user avatar
  • 44.1k
8 votes
4 answers
13k views

Convert XML to CSV

I'm pretty sure this code can be optimized, but I'm not talented enough in Linq to do it myself. Here's what I'm trying to do: I have an XML file that needs to be converted into a .csv file. The XML ...
Phrosen's user avatar
  • 111
4 votes
1 answer
108 views

Filtering Gantt chart data involving hierarchical DTOs

I have a bunch of code that deals with modifying a large nested set of IEnumerable<DTO's> Basically, this is the structure of the ...
Train's user avatar
  • 301
5 votes
2 answers
22k views

LINQ and string.Split do it yourself practice

Problem Statement: Implement a sentence scanning functionality to parse the sentence and return a string to concatenate each word with its number of occurrences, sorted by the number of ...
Jianmin Chen's user avatar
  • 2,396
2 votes
1 answer
7k views

Recursive statement with linq

I have a recursive statement that removesactivities that don't have a resource type for a certain ...
Train's user avatar
  • 301

1 2
3
4 5
13