All Questions
Tagged with linq-query-syntax or linq
86,794 questions
2
votes
1
answer
107
views
Entity Framework/Linq Get Last Record From List With Condition
I have a list of records, by multiple UserID's.
TableID UserID Status
------------------------
1 3 3
2 4 3
3 3 4
4 4 3
5 5 4
6 ...
0
votes
2
answers
153
views
MRU structure with fixed number of elements, automatically adapting to LINQ queries
I have a folder containing huge numbers of files in many subfolders, and I want to select the files with a name that matches any of a specified list of patterns (regular expressions). My regular ...
7
votes
1
answer
260
views
Difference between Expression.Return and Expression.Goto in C# Expression Trees
I'm experimenting with C# Expression Trees and trying to understand the difference between Expression.Return and Expression.Goto. I can’t create an example where Return and Goto behave differently ...
5
votes
6
answers
194
views
DistinctBy, but preserve the last element of each group of duplicates
I have a list of items that contains duplicates, and I want to keep the last item of each group of duplicates. The native DistinctBy LINQ operator keeps the first item from each group, so it doesn't ...
0
votes
3
answers
115
views
C# Remove Both Duplicate Values from Custom Object Based On Multiple Positive/Negative Values
Working in C# with a custom object that has multiple rows and columns.
I need to remove duplicate records where some (but not all) of the column values match, including positive and negative values of ...
1
vote
1
answer
101
views
How to use expression setters in ExecuteUpdate again in EF Core 10
Microsoft have changed ExecuteUpdateAsync to accept non-expression setters parameter in NET 10. Now it works this way:
await context.Blogs.ExecuteUpdateAsync(s =>
{
s.SetProperty(b => b....
Advice
0
votes
5
replies
105
views
C# & EF Core nullable type return vs not found or multiple database rows
I have the following use case: the database contains a row with a nullable column, and I would like to request only that column value from the database for a single row.
This can easily be done using ...
-5
votes
2
answers
200
views
A list object gets the last five years of an object [closed]
Is there a way I can get the last five years of an object?
I been trying to use Max to get the maximum year and then use range to get all the objects within that range:
using System;
using System.Linq;...
1
vote
1
answer
108
views
How would I use N.EntityFramework.Extensions to insert into another table
I am trying to use the InsertFromQuery() to add data to a SQL table:
await cx.Users
.Where(x => x.Country == "Romania")
.InsertFromQueryAsync("Mailbox", mb => ...
-6
votes
1
answer
157
views
Can someone please assist with a C# LINQ question? Sorting is not working [closed]
I'm trying to get a multi line graph with Google charts. I am having an issue with sorting the data properly.
My data is as follows:
public class TemperatureReading (extra only, more fields present)
{
...
1
vote
1
answer
88
views
EF Core populating its navigation although it shouldn't
I am reading the book C#12 In a Nutshell. The author says:
Loading navigation properties
When EF Core populates an entity, it
does not (by default) populate its navigation properties:
using var ...
3
votes
1
answer
125
views
Get top 10 with the highest sum of column A, While the sum of column B does not exceed 625
We have a data table that have some data in column A, column B, column C (id).
Note that the list is near 250 items. Here's it's a small representation
In c# it gives a list like this:
List<Item>...
2
votes
1
answer
152
views
Deserialize complex JSON with data in keys
I am building an editor script for Unity to import a complicated blueprint JSON asset extracted from another game engine. Some of this data is stored in the keys, meaning I cannot follow a typical ...
1
vote
1
answer
94
views
Why does await Task.WhenAll() with LINQ Select lose exception stack trace?
I’m using Task.WhenAll with LINQ to run multiple async operations. However, when an exception is thrown inside one of the tasks, the stack trace seems incomplete compared to when I await each task ...
-5
votes
2
answers
143
views
Why do I need to assign the result of a order by LINQ query to a variable to get the sorted list? [duplicate]
Decided to take a test in C#, I have a class called Purchase which has three string properties namely CustomerName, Item and Amount. I created my own implementation of the Purchase class whose details ...
0
votes
2
answers
128
views
C# LINQ - Splitting a List into two variables by a condition [duplicate]
Let's say I have someList of things I want to split into two separate lists by some condition.
I've thought of 3 options:
// 1 - two Where's
var trueGroup = someList.Where(x => x.SomeCondition);
...
-2
votes
1
answer
141
views
Retrieve previous record to current list for calculation
Goal:
Get previous record to current list for calculation using LINQ
Result:
Date | EatenFoodInKg | CompareToFoodFromYesterday
2000-01-11 10 9 (10-1)
2000-01-10 ...
3
votes
1
answer
164
views
Why is a simplified version of Max(decimal) slower than Linq's implementation?
I'm working with some benchmarks, and I noticed some odd behaviour with my methods vs. Linq's implementation. Testing on .NET 9, X64 RyuJIT.
I'm determining the max value within a decimal array. In ...
7
votes
2
answers
283
views
How to reliably find out if an IEnumerable<T> is materialized?
I'm used to have a method in my code base, that basically does something like:
return source as IList<T> ?? source.ToArray();
where source is an IEnumerable<T>. I used this method to make ...
2
votes
1
answer
79
views
Gets AggregateException on LINQ extension method using LINQKIT
I'm creating a LINQ extension using LINQKIT, but I can't figure out why I get this error
System.AggregateException: One or more errors occurred (no method 'InGroupsImpl on type 'SLT.Assets.Extensions....
-2
votes
1
answer
69
views
Linq sort entity if JSON value exists [closed]
I have a db table column where some properties are stored as json. Here is an example :
properties
----------
{ "SortBy" : "ASC" }
List<MyItem> items = await this.dbContext....
0
votes
3
answers
191
views
How to use custom functions on LINQ statement?
I have an extension that returns true or false if value is null (null == false), called HasValueLite().
When I use the method in a LINQ statement, I get:
System.AggregateException: 'One or more errors ...
1
vote
3
answers
133
views
C# list of object search multiple time dynamically
I have a list of object and one item can have multiple value inside.
I want to search dynamically the "theme" field with multiple criteria.
So, my list is :
class book
{
public int ...
1
vote
2
answers
109
views
Combining multiple unknown queries using Union
I have an Entity Framework Core statement where I need to create a union of multiple selects. The number of selects is variable.
I would like to do something like this, but it is not working:
public ...
0
votes
0
answers
57
views
Splitting string into chunks of given size but only splitting at newline characters [duplicate]
I have this code now that splits by size:
IEnumerable<string> chunked = Enumerable.Range(0, statusMessage.Length / CHUNK_SIZE)
.Select(i => statusMessage.Substring(i * CHUNK_SIZE, ...
1
vote
1
answer
109
views
Is there a solution for deserialization problem in NRules in combination with LINQ? [closed]
I would like to manage the rules that are to be checked with NRules in a database and read them in dynamically. I therefore need the rule definition in JSON format.
For testing purposes, I have used ...
1
vote
2
answers
98
views
.NET 8 Performance problem iterating over 100000 records
I have a performance problem processing 100000 client records. When I use a Linq query of looping through the orders it takes about 9 minutes. When I comment out the Linq query or the loop, the ...
1
vote
1
answer
75
views
c# LINQ Select the last element from several identical ones [duplicate]
There are identical values in the column Bookings.TimeslotId.
We need to group them (OrderByDescending?) and to take the last value (LastOrDefaultAsync?).
I think this is a subquery on one line of ...
3
votes
1
answer
95
views
Sorting a List based on Start and End dates with no Overlapping dates
I have a concept which I have trouble into converting it into code.
The basic idea is as shown below image
Display data with non-overlapping dates
The picture might not say much. To describe the issue,...
0
votes
1
answer
62
views
How to sort a collection of XElements by their XElement name
I have the following XML; I read it as a collection of XElements:
<rql:select rdf:parseType=""Resource"" xmlns:rdf=""http://www.w3.org/1999/02/22-rdf-syntax-ns#"&...
0
votes
2
answers
573
views
C# LINQ Dynamic - Against a DataTable - Syntax not working?
My app allows the user to choose what table and columns to filter on. It can be a different table and columns each time the user runs this routine. So standard LINQ will not work. I need to do this ...
0
votes
2
answers
73
views
EF Core Fluent API to get inheritance properties
How to use EF Core Fluent API to get inheritance properties?
I have a base class and 2 inherited children classes:
class BaseClass
{
public string Name { get; set; }
}
class Child1 : BaseClass
{
...
1
vote
2
answers
112
views
C# LINQ Query WHERE Record ID IN list of IDs from another table
I am new to LINQ query and trying figure out how to filter list of records based on the list of IDs found in the sub-query inside. Basically, I am looking for the option similar to SQL "IN" ...
0
votes
1
answer
103
views
Using linq to get the integer into the string
I need to get the id into the string with comma from the object. I used linq but the string is
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[item,System.Int32]
There are my two objects:
...
1
vote
1
answer
65
views
Query XML on two levels
I need to check if an account with "productNumber" = 90 and "sequenceNumber" = 1 exists - but only if the "status" (on the level above) = ACTIVE. I have managed to get ...
0
votes
0
answers
108
views
Parsing .ini file using Linq
I am trying to parse an ini file and grab specific key-value pairs,given a section name and key name. I use the following code, using Linq, and it works if I don't include the key name in its search ...
1
vote
1
answer
133
views
Why this Linq doesn't equals null?
I'm working on a project that uses the legacy .NET Framework 4.x. I'm using LINQ here, but it's not behaving as I expected, and I'm not sure what mistakes I've made.
I have the following MySQL table:
...
-1
votes
1
answer
128
views
Change Excel spreadsheet by modifying underlying XML files
I'm trying to edit a spreadsheet by diving into its underlying XML files after I read the XLSX file as a ZipArchive.
It's for a work project where we're trying to get rid of Excel running in the ...
0
votes
1
answer
225
views
How do I make OData parse a filter string into an Expression?
I have a filter string conformant to the OData $filter syntax and wish to parse it into a System.Linq.Expressions expression.
Context: The web API of our cloud application offers querying endpoints ...
0
votes
2
answers
132
views
How do I compare two lists of objects in C# where a field matches for all rows
I have two lists of the same object type with the columns:
CustomerID
ProductID
33, 120A
33, 240B
33, 14CD
33, 984A
34, 120A
34, 240B
35, 14CD
35, 984A
39, 120A
39, 240B
39, 14CD
39, 984A
How can I ...
-1
votes
1
answer
93
views
How to perform multi-level sorting, field selection, grouping, and counting using LINQ method syntax in C#?
I'm working on a C# application where I need to manipulate a list of Employee objects using LINQ's method syntax. Specifically, I'm attempting to:
Sort the list first by ProjectId in ascending order, ...
1
vote
1
answer
88
views
LINQ query not grouping and when returning, missing data
I have grouped orders from the orders table so an order can have one or more purchases of a product with in an order. The problem is that when I purchase a single product that order with one product ...
2
votes
2
answers
73
views
LINQ query with nested list using Groupby and First
I have a stored procedure returning results in a FlaggedCarDTO object:
public class FlaggedCarDTO
{
public int CarId { get; set; }
public int FlagUserId { get; set; }
public int Year { get;...
1
vote
1
answer
71
views
"inheritance" between mappings using Expression<Func<...>> for Entity Framework queries
A rather complicated object mapping I've inherited uses static Expression<Func<...>> in the .Select call (from my research these seem to be called "Expression Trees", but for ...
1
vote
2
answers
122
views
Does ToList() runs separately when used in a select projection? [duplicate]
I am working on a legacy codebase where there are tons of repository methods that seem to be thread-blocking. I know calling ToList(), FirstOrDefault() over an IQueryable causes a synchronised fetch ...
0
votes
0
answers
86
views
How to convert a Func to an expression? [duplicate]
I have the following:
public Func<MyObj, bool> GetFilter(Status status) => status switch
{
Status.Active => x => x.Status == Status.Active && x.ValidFrom <= DateTime....
2
votes
1
answer
112
views
Implementing a LINQ Expression Visitor to Remove Pagination
I am trying to implement a LINQ Expression Visitor ([ExpressionVisitor][1]) for the following scenario: remove all pagination-related calls, such as Skip, Take, OrderBy, and OrderByDescending.
What I ...
0
votes
2
answers
80
views
Get Sum of Value on Grandchild [duplicate]
So, I have some code here that's pretty straight forward in what I'm looking for. I'm trying to sum up a value from two (and possibly more) levels deep of objects. I can do it with the displayed ...
4
votes
2
answers
187
views
What is time complexity of LINQ Order()/OrderBy() followed by Take(k)?
What is the time complexity of the following code in C#?
array.Order().Take(k).ToArray();
Will LINQ treat this as QuickSelect? Or, will it perform full array sorting with the complexity O(n log n)?
2
votes
1
answer
147
views
Linq.OrderBy produces different order on different machines
I am ordering a list of files by their name.
My problem now is the order is different when running the same code in a Docker container (base image ubuntu/dotnet-aspnet:8.0-24.04_stable_130).
My setup: ...