All Questions
Tagged with linq-query-syntax or linq
86,794 questions
0
votes
2
answers
84
views
How to flatten a nested entity using Linq (opposite of SelectMany)?
I am trying to convert SQL to Linq that flattens two related tables into one record per parent table row (opposite of SelectMany).
My first attempt is below, but the resulting SQL is different ...
0
votes
1
answer
39
views
LINQ - DataTable GroupBy Count Distinct
I am trying to join to tables together in vb.Net using linq to return the distinct count of the primary keys, and I can't quite seem to get the syntax
Here's the code to create the tables
Dim ...
1
vote
1
answer
35
views
Using automapper with dynamic linq and order by clause
I'm using automapper for a dynamic query and it's working well until I use an orderby clause.
So this is how I create the query:
IQueryable<DTOFormCellSouches> query;
//Configuration de l'...
1
vote
3
answers
64
views
How to modify a feature tag attribute depending on a value of a different attribute with LINQ
I have the following source as XML:
<?xml version="1.0"?>
<report>
<feature tag="Config"/>
<feature tag="Runtime">
<feature tag=...
1
vote
2
answers
63
views
Remove a particular XML element depending on a particular attribute value with LINQ
As source I have a XML document with the following content:
<?xml version="1.0"?>
<report>
<feature tag="Config"/>
<feature tag="Runtime">
...
1
vote
1
answer
81
views
selecting a parent object list based on conditions of derived or sub object list
I have a collection of UserTaskGroup as List, in which each UserTaskGroup object have a List in it, means for 1 task group, there could be one or more tasks. And UserTask class has a field named ...
0
votes
2
answers
124
views
How to use LINQ to compare two complex lists
I never used LINQ but now I have to and I'm having a bit of trouble with it. Below is an example of how I would normally compare two lists and retrieve the bad and good price records. How can I do the ...
-1
votes
2
answers
96
views
How do I update a list<> from another list<> with two conditions using linq?
How can I update the following approach using linq to update a loop within a loop, and having two conditions? I've looked at other posts that are similar but not with two conditions.
foreach (var ...
2
votes
1
answer
129
views
Parallel.ForEachAsync does not change properties of IEnumerable items [duplicate]
I have the following class
class Person
{
public required string Name { get; set; }
public required string Lastname { get; set; }
public override string ToString() => $"{Name} - {...
-1
votes
2
answers
200
views
What are the performance implications of using AsParallel() with Parallel.ForEach() in LINQ queries?
To optimize the performance of a C# application and came across AsParallel() in LINQ.
I want to understand the key differences between them, especially regarding performance when working with larger ...
2
votes
2
answers
176
views
How to generate an inverse dictionary in C# [closed]
Let's have a phone book:
"John" ---> 6463
"Andrea" ---> 1234
"Michael" ---> 2611
"Irene" ---> 6463
"Matthew" ---> 7699
Notice that ...
2
votes
1
answer
62
views
Empty enumeration from Enumerable.Select; or why does its enumerator seem to behave differently from other enumerators?
I am writing a library function to split an enumeration into multiple spans of enumerations but am running into an inexplicable issue with the enumerator from linq Select clause. Please see ...
0
votes
2
answers
106
views
Linqpad is not submitting changes
I don't know what i miss. I used it thousands of time and I updated millions of rows before.
There are two tables. One is storing definitions. One is storing relations years based. Some relations are ...
-1
votes
2
answers
184
views
Getting an error that the LINQ expression could not be translated. Either rewrite the query in a form that can be translated [closed]
I have C# application (.NET Core 4) and I have written the following LINQ expression for getting list of assigned tickets from database view.
public async Task<List<TicketSchedulerDTO>> ...
2
votes
1
answer
55
views
XML LINQ matching trouble [closed]
XML:
<DLC>
<ObjectClass>RVM_GWR_Class166_DMSL_C</ObjectClass>
<ShortName>Class166</ShortName>
</DLC>
<DLC>
<ObjectClass>...
-1
votes
2
answers
95
views
Modify LINQ Query to Include Averages in Grid Display
I have the following LINQ query:
var resultData = from audit in _context.audittable
join region in _context.Regions on audit.RegionCode equals region.RegionCode
...
2
votes
2
answers
110
views
LINQ - How to correctly group a list of users by their nested list of values
I have List<User> with this dependencies:
class User
{
public string Id { get; set; }
public List<ManagerGroup> ManagerGroups { get; set; }
}
class ManagerGroup
{
public string ...
0
votes
1
answer
63
views
Extract a strongly typed expression from a function expression body
I'm trying to write a "relaxed" comparison method which operates on Expressions and is used in Entity Framework. It consists of a chain of Replace() calls and ToLower() to perform an inexact ...
0
votes
2
answers
150
views
Microsoft.Data.SqlClient.SqlError: Incorrect syntax near the keyword 'AND'
This is the error I'm getting when I trye to execute the code.(https://i.sstatic.net/Z2MVQ0mS.png)
Error message: Incorrect syntax near the keyword 'AND'.
StackTrace:
at Microsoft.Data.SqlClient....
1
vote
2
answers
99
views
How can I add multiple conditions in a LINQ Select to return values based on an enum?
I'm working on a LINQ query where I need to select values from a data source based on the value of an enum (ConversationRole). I want to return different user IDs based on whether the ConversationRole ...
0
votes
0
answers
58
views
How to exclude records with empty collection in linq query [duplicate]
I have 3 tables in postgres DB (docker postgres:14.1-alpine).
public class House
{
public Guid Id { get; set; }
public virtual ICollection<Flat> Flats { get; set; }
public ...
0
votes
1
answer
83
views
Why EF Core LINQ query works okay for one child and does not work for another one?
So I have a query to get a list of chats with 2 users (including their pictures) and the last message.
An exception is thrown because of that last message sub-query:
"The LINQ expression '...
1
vote
2
answers
285
views
How to avoid materializing a query prematurely in Entity Framework Core
I am working with the ABP Framework using C# and Entity Framework Core, and I have the following query:
public async Task<ListResultDto<GetLinkedOrganizationsDto>> ...
2
votes
2
answers
52
views
Split items to a different group each time a value changes with Linq
I want to split the items using Linq in such way that each time a command type changes the previous items go to a separate group.
I tried to split using GroupBy but it just puts all items with same ...
-2
votes
1
answer
70
views
C# Linq. Join collections by ID
I've got three collections with common key:
var shops = new[] { new { shopId = "1", shopName = "shop1" }, new { shopId = "2", shopName = "shop2" } , new { ...
0
votes
1
answer
60
views
EF Core - Nested property not appearing in Json
I am working on Odata API with .NET. I am facing an issue with the implementation of one of my endpoints. I have a nested array that is not being returned in the JSON response. The /software service ...
0
votes
1
answer
66
views
How to convert join 2 tables with group by into linq
How could I convert this query into Linq in C#?
SQL query:
SELECT *
FROM com.DepartmentPersonnel q1
INNER JOIN
(SELECT dp.DepartmentID, MIN(dp.JobTypeID)JobTypeID
FROM com....
0
votes
1
answer
28
views
Receiving Master Data Type as a result
How to get parent model as a result. I want to get MasterData type as a result. i want to use where condition in Attributes but i want to receive Master Data Type as a result
var result= MasterData....
0
votes
0
answers
72
views
Distinct method ony works when modifying string
I am having an issue with the Linq Distinct Method. I have a List of LocationID's
B A01A
B A01A
A A01A
A A01A
A A01A
As you can see, there are only two distinct values here. However, When they are ...
0
votes
1
answer
74
views
Linq - How to query json inside Linq
I have this code:
List<int> cats = new List<int>();
foreach (var cat in ctx.TblCategories)
{
int catCount = ctx.TblProducts
.Where(x => int.Parse( ...
1
vote
1
answer
83
views
Add an aggregate pipeline stage changing to another model or type in MongoDB C# driver
How to add an aggregate pipeline stage that will change the collection model output that the aggregation is being run on?
X/Y question: How to aggregate lookup a document referenced as ObjectId in ...
2
votes
4
answers
254
views
Is it possible to use LINQ to query for elements occuring in a certain sequence?
This is hard to search for as a non-native English speaker, as any search terms I come up with results in lots of questions related to OrderBy(Descending), and not what I am after.
Imagine I have a ...
1
vote
1
answer
58
views
Get child node values from Linq C#
Trying to get values of some child elements in XML, however, when retrieving the values of these elements, they are stitched together.
Also, sometimes the file can have 1 reference number but there ...
0
votes
1
answer
112
views
Why are my GUID type filters returning null?
In my .NET Core application using Entity Framework Core I was using SQL Server but am shifting to SQLite. In SQLite GUID type filters are returning null:
public Guid GetAdminBranchId(Guid companyId)
{
...
0
votes
1
answer
59
views
Dynamic Linq GroupJoin Issue
I have a big question about the use of conditions in group joins using Dynamic Linq Query syntax.
I have the following code, which I am using to generate a left join code.
result = result.AsQueryable()...
0
votes
1
answer
89
views
Mock GroupBy method with in-memory database
The following method works perfectly with SQL Server, no issues.
public async Task<IEnumerable<HashRecord>> GetLatestHashes(long jobId, CancellationToken token)
{
var context = ...
1
vote
1
answer
102
views
Linq: Expression not supported: a => new AnonymousType because expression cannot be translated as a field path
I am trying to write a linq query against the Mongo DB C# driver in order to pull back documents from a collection where they do not have a corresponding related record in another collection. A is an ...
1
vote
2
answers
167
views
Shortest way to set a property after FirstOrDefault
Given request.Order type is ColumnName[]. I'm trying to write a one line piece of code equivalent to this incorrect code:
request.Order.FirstOrDefault(x => x.Name == "From").Name = "...
-1
votes
2
answers
91
views
Subgrouping before final grouping [closed]
I have SQL query where I subgroup 4 items before final grouping. Here is the SQL query :
Select DISCIP,
SUM(TOTAL) as TOTAL,
SUM(COMP) as DONE,
SUM(...
-3
votes
1
answer
124
views
Force EF6 to use VARCHAR instead of NVARCHAR in QUERY expressions [duplicate]
Disclaimer - This is both a theoretical and real case question. Legacy and other circumstances makes it real.
The question is:
Can I force EF6 to use VARCHAR instead of NVARCHAR in QUERY expressions?
...
0
votes
1
answer
80
views
SQL -> LINQ - Multiple left joins with null values
I want to convert this sql query to LINQ
left join @categories oc
on OC.course_id = t.course_id
and isnull( t.offlinecategory_id,-1) = isnull(oc.offlinecategory_id,-1)
I tried in ...
0
votes
0
answers
66
views
Convert a LIST specific 2 properties result to a 2D array
I've a list of class containing different type of properties:
public class Test
{
public string grade { get; set; }
public int minVal { get; set; }
public int maxVal { get; set; }
}
...
0
votes
1
answer
106
views
I need to group by datetime but in specific timezone with EF Core and PostgreSQL
I need to group transaction by its CreatedAt property. But in the specific timezone.
Like this
SELECT
SUM("Amount")
FROM
public."Transactions"
GROUP BY
CAST("...
1
vote
1
answer
217
views
C# LINQ OrderBy with an explicit culture returns different results on different .NET versions
I have a list of English and Korean words that I am trying to order so that the Korean words are ordered first, with the English words being ordered last.
This SO post is my exact issue: C# linq order ...
0
votes
1
answer
58
views
How can I use LINQ to filter from a list inside of another list?
Alright, so this is quite hard to explain, but I will do my best.
I have a list filled with books.
All of the books have variables such as Title, publishYear, and Author.
But they also have Genres, ...
0
votes
4
answers
278
views
Is there a way, using Linq, to iterate through a list, check a condition, and send successes and failures to 2 different outputs?
Using Linq, you can iterate through a list, taking all items that meet a condition by using the Where method. However, I have a list in which I want to take all items that meet a condition and do one ...
0
votes
2
answers
103
views
C# idiomatic `myList.All((item, index) => {...})` [closed]
I've got a List<> in C# and I want to check that all elements of the list meet some condition. This condition depends on the element in the list, as well as that elements index in the list.
It ...
8
votes
1
answer
308
views
.NET 8, EF Core query could not be translated when using a static collection/list as field
I'm curious about the following:
Why is EF Core unable to translate a static readonly ICollection<T> & static readonly IList<T> field into a query when using it inside of a .Contains()...
0
votes
1
answer
49
views
EF Core : Where not working as expected (caching?)
I have this situation:
SQL Server database used with EF Core (code-first strategy).
I have an entity containing a boolean flag called Inactive - with the meaning: when TRUE, this entry is regarded as &...
-2
votes
3
answers
116
views
Only parameterless constructors and initializers are supported
I have a method where I changed the endswith logic with a conditional base as follows from then it starts throwing the exception
public async Task<IEnumerable<CorpTransaction_DTO>> ...