Questions tagged [linq]
Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages. Please consider using more detailed tags when appropriate, for example [linq-to-sql], [linq-to-entities] / [entity-framework], or [plinq]
47
questions
0
votes
0
answers
14
views
Improving performance of contains method in ASP.NET MVC web application with large dataset
I have an ASP.NET MVC web application using C#, Visual Studio, and SQL Server. This is my code:
if (!string.IsNullOrEmpty(Name))
{
query = query.Where(x => x.PatientName.Contains(Name));
}
The ...
0
votes
1
answer
41
views
2 LINQ queries use the same joins. Is there a way to "factor out" the joins and gain a performance benefit?
Here are two example queries:
var queryA =
from t1 in context.T1
join t2 in context.T2
where
tl.colA == desiredColAValue
select new ObjectA(valuesFromQueryA)
;
var queryB =
...
2
votes
2
answers
39
views
How to handle the possibility of expected null properties in a LINQ were clause?
Consider the following C# Code Fragment:
public class Person
{
public int Id { get; set;}
public string? Name { get; set;}
public string? Description {get; set;}
}
List<Person>? ...
0
votes
0
answers
59
views
How do I set SQL Provider in C# Linq
I changed the default collation of my SQL Server 2022 database (and all string objects that already existed) to Latin1_General_100_CI_AS_SC_UTF8.
Unfortunately, Linq seems unable to handle this ...
-1
votes
1
answer
41
views
Insert prefix in a Xml string [duplicate]
I need to be able to write this Xml string:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:a="http://www.w3.org/2005/08/addressing"
...
1
vote
1
answer
54
views
C# linq Include OrderBy
I need some help sorting data by Included data. In the example below, I am returning a purchase order where I want to sort the purchase order line items by the line item id.
return await context....
-1
votes
1
answer
56
views
Equals method never called by LINQ expression [duplicate]
My data class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using static iText.StyledXmlParser.Jsoup.Select.Evaluator;
...
1
vote
4
answers
46
views
Apply Group by to dictionary value
I have a Dictionary which key of string type and value of list of integer type, like this:
Dictionary<string, List<int>>.
Lets say I add elements to it:
Dictionary<string, List<int&...
-1
votes
0
answers
63
views
Performance degradation of linq query in C# after .NET Core upgrade to 8 [duplicate]
I have the simple linq query in my .NET Core C# app. We have upgraded from .NET Core 3.1 to .NET 8.
My query is as follows:
Decimal[] cks = mem.Select(a => Convert.ToDecimal(a.KEKE_CK)).ToArray();
...
0
votes
0
answers
93
views
Why this C# code runs slow if the loop is more than 30 times? [closed]
int range = 20;
var test = Enumerable.Range(0, range).Select(i => new Section { Id = i, Value = i }).ToList();
var result = RemoveEvenNum(test);
foreach (var section in result)
{
Console....
1
vote
1
answer
52
views
LINQ group with list inside shows only the first row
I have a view in the v_PortalProprietario database that returns in each line the name of the owners that are repeated and the accounts (Acertos) that are unique. I would like to make a query in Linq ...
0
votes
1
answer
59
views
Convert List<Task<T>>> to List<T> [duplicate]
I have this variable that should project and return a list of AttachmentRequestModel as follows :
var mappedAttachments = recordAction
.PPAttachments
.Select(async x => new ...
0
votes
2
answers
55
views
How to join tables and group the results using LINQ?
I have a table of Products and a table of Product_Images with a one to many relation (one product can have multiple images) and I am trying to retrieve all products with their related images. So far I ...
0
votes
1
answer
59
views
Entity Framework Core : Include not working for many-to-many relationship
I'm having trouble with .Include() not working for many-to-many relationships in Entity Framework Core. My setup works fine for one-to-many relationships, but the related entities are not being loaded ...
0
votes
1
answer
54
views
Pagination on the result from multiple collection .net
I am trying to add offset pagination( input is pagesize and limit) on combined result which is aggregating multiple collections. Earlier I had only limit on the reponse. Below is the code for the API ...
0
votes
3
answers
79
views
Sequence contains no elements in LINQ
In the code shown here, I am getting an error in LINQ:
Sequence contains no elements
When the reason column contains data, it is throwing this exception. Please suggest how to handle this exception.
...
0
votes
3
answers
58
views
Entity Framework C# join with one column only from second table
I have a join in Entity Framework connecting the Order and the OrderItem tables. All orders will have multiple order items. I'm trying to return all the orders, but only the orderItemStatus column ...
-2
votes
0
answers
29
views
Npgsql.PostgresException: '42703: column c.UserId1 does not exist [closed]
var list = _user.FindAll(
filter,
NoTracking: true,
hideDeleted: true,
take: data.Length,
page: data.Page,
orderBy: data.OrderString);
Error: Npgsql.PostgresException: '42703: ...
-1
votes
0
answers
53
views
Linq query for same table parentId
This is my table structure:
enter image description here
Now I want to result like this:
enter image description here
I wrote a SQL query shown here, and it returns results as expected, but I want it ...
0
votes
1
answer
30
views
The cast to value type 'System.Int32' failed because the materialized value is null LinQ
I do have below query,
(from p in _dbContext.MyTable
where batchIds.Contains(p.BatchId)
select new ParticipantToRespond
{
ParticipantId = p.ParticipantId,
TeamId = (p.TeamId ??...
1
vote
1
answer
49
views
How to delete the rows of datatable1 having different ID while compare with the datatable2
Given the first data table having master data and the second table having the needed data IDs, I need to delete the rows from the first table which are not present in the 2nd table. I am trying to use ...
-1
votes
0
answers
72
views
when converting the code from csv to xml, the text does not transfer from the file
When running the code, a terminal is launched in which all the text from my file is shown, but then when opening the file values is not there. What could be wrong.
About my code :
I try to convert CSV ...
0
votes
0
answers
28
views
Use some kind of helper function In EF Select query to make subquery [duplicate]
I have several tables that handles different kinds of translations for my entities and those tables to linked via relations to other entities.
I use subquery to directly access translations tables ...
0
votes
1
answer
49
views
How to group text to lines if there is small difference in Y position
Using PdfPig to read text from order PDF as described in Get Text Line By Line From PDF using C# answer
static void GetWordsInReadingOrder(UglyToad.PdfPig.Content.Page page, StringBuilder builder)
...
-1
votes
1
answer
61
views
LINQ query where clause in list
I am trying to limit the data returned via a repository using the where clause based on a list of values.
I have a PurchaseOrders entity where I want to return all purchase orders that have an ...
0
votes
3
answers
78
views
Selecting 2 Elements from XML
I am having a lot of trouble translating examples I am finding into use with XML.
Starting with this (a very cut down version of the whole XML file)
<?xml version="1.0" encoding="UTF-...
0
votes
2
answers
55
views
LINQ - Get key-value pairs for dictionary with lists
Let's say I have:
Dictionary<string, List<string>> test = new()
{
{ "animal", ["cat", "dog"] },
{ "fruit", ["banana", "apple&...
0
votes
0
answers
78
views
Linq query DateOnly fields returning results on comparing dates
I have this linq query request as:
var test = (...
where ...
&& test.SL.Any(line =>
line.ServiceCode == serviceCode
...
0
votes
2
answers
45
views
How to use Enumerable.Aggregate without getting CS0411 error
There are two DTO records:
public record struct DateHour(int Hour, double? Value);
private record struct HourSnapshot(uint TotalSeconds, double? Value);
I have another method which works with enum. I ...
3
votes
1
answer
71
views
Remove In Nested Loops (Remove Inside Foreach) [closed]
These are my entities:
public class Permissions
{
public string PermissionName { get; set; }
public List<Controllers> controllers { get; set; }
}
public class Controllers
{
public ...
0
votes
1
answer
55
views
System.Xml.XmlException: 'The ' ' character, hexadecimal value 0x20, cannot be included in a name.'
Wrote the code converter csv file to xml file and when i started it i have an error:
internal class Program
{
private static void Main(string[] args)
{
var csvFile = @"sbor.csv&...
0
votes
0
answers
30
views
Search implemented using LinqKit predicateBuilder working fine in the local but not working in the dev build
I have implemented a search functionality using the LinqKit predicate builder. It is working fine in the local environment and when it is published to a folder the same functionality is not working. ...
0
votes
0
answers
55
views
Is there a way to convert a LINQ expression to another LINQ expression?
My repository class has a few methods for operations with the database. I recently developed the FindAsync() method, which allows you to grab only a few rows from the database based on an expression.
...
1
vote
1
answer
89
views
EF Core : retrieve products with all supplier prices for price comparison and identify cheapest supplier
I'm working on an ASP.NET Core application using ABP framework and EF Core. I need to retrieve information about products and their suppliers for comparison purposes.
Specifically, I want to achieve ...
0
votes
1
answer
55
views
How to access a XML Parent Attribute with LINQ in C#
I cant seem to get the Linq syntax correct - or I dont know how to reference XML objects.
I have XML like
<?xml version="1.0" encoding="UTF-8"?>
<Targets>
<...
1
vote
2
answers
120
views
Multiple tables query with repository pattern
I have an ASP.NET Web API project, and have implemented repository and unitOfWork patterns.
I currently have a big SQL query that I want to implement in code, it includes a lot of tables and joins.
My ...
0
votes
0
answers
21
views
Coerce generic argument of expression
I have a situation where I need to logically-combine two expressions:
Expression<Func<T1, bool>> expr1; Expression<Func<T2, bool>> expr2;
In some cases, T1 and T2 are the same ...
0
votes
1
answer
30
views
LINQ returning null for some xml read
My xml read with linq used to work great, but I recently changed my xml (the team wanted flatter xml), and the format is like this now:
<?xml version="1.0" encoding="UTF-8"?>
...
0
votes
1
answer
51
views
Why does adding .AsQueryable() and .AsNoTracking() seem to work for filtering related entities in Entity Framework Core? [closed]
I need to filter related entities while including them in the query.
Each Order has a Customer, and multiple OrderItems, and each OrderItem has a Product. I want to include OrderItems that either ...
0
votes
1
answer
82
views
Using Linq to select multiple values with reflection VB.NET
I'm experimenting with some LINQ in which I'd like my filters to be dynamically loaded at run time and for the result set to be returned as a 2d list of:
Type
Link
Google
[email protected]
...
-1
votes
0
answers
74
views
Join in-memory list and DB table in .net 8
I have a code that is working in .Net 3.1 and we have upgraded to .Net 8 and as part of that I have converted Nuget package Microsoft.EntityFramework.MemoryJoin to 0.8.0 and I observed the in-memory ...
-1
votes
0
answers
55
views
Link collection string not matching a string
I have a collection from a api call where i have field called links, as below image
Now I need to loop through these 100 records like this and match with the company number that i have.
To achieve ...
0
votes
0
answers
92
views
Dynamic LINQ sorting after grouping
We wrote a generic filterable repository in .NET 8 that should be able to accept a list of filters and a list of sorts, and apply them to any given entity.
Due to some business logic, we have a Task ...
1
vote
1
answer
56
views
Join tables based on StartsWith function
I have two tables to join. The joining condition is a column that should be like or starts with the other column, not exactly equal. How to do that in Linq? This is my function:
public async Task<...
2
votes
2
answers
80
views
How can I reuse the logic in a .Include.Where() call while working with Entity Framework Core in .NET?
I am writing a database query using LINQ in .NET and I want to be able to not duplicate the code I put in my Where method calls.
I want to return Blogs that have fresh Comments, and want to filter ...
0
votes
1
answer
46
views
Fetch the Name based diff version for same Service and Function
Here is my Json object .
[
{
"Id": 1,
"Name": "Asset A",
"Version": 1,
"Function": "Song",
"Service": "Song"
...
0
votes
0
answers
34
views
EF Query Translation failure if using constructor but not if using property initialization
When performing a somewhat convoluted query I get a "could not be translated" exception if I am selecting into a class that uses a constructor. Oddly I get a success if I use the same class ...