Questions tagged [linq]
Language INtegrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.
604
questions
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 ...
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 ...
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 ...
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:
...
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 ...
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:
<...
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 ...
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.
...
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?
...
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 ...
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:
...
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 ...
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:
...
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 ...
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:
...
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 ...
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:
...
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:
...
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:
...
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 ...
8
votes
1
answer
3k
views
GroupBy(params string[] fields)
Linq to Object GroupBy extension method allows grouping by projection:
...
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 ...
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 ...
-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.
...
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 ...
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 ...
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:
...
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. ...
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, ...
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 ...
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 ...
-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 ...
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:
...
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 ...
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 ...
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:
...
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....
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.
...
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, ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...