6,867 questions
3
votes
1
answer
253
views
How can I project an entity in such a way that it remains filterable on the database?
I am trying to project the result of the following query into EF Core 8 entities:
select foo.*
, (case when hst.Id is not null and foo.OtherColumn = 1 then 42 else null end)
as Bar
...
1
vote
2
answers
143
views
Reverse a LINQ to Entities query?
How do I use Reverse to reverse the order of the results of a LINQ to Entities query?
Something like:
var result = dbContext.Users.Where(a => a.Name != null).Reverse();
2
votes
2
answers
116
views
AsNoTracking when selecting Non-Entities
Still on EF 6 "classic" (not core), when I do something like the following:
var basicPersonList = myContext.Persons
.Select(p => new PersonSimpleData
{
FamilyName = p....
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
1
answer
45
views
How to get data when need to join table and only select the latest item
I have this query in SQL:
SELECT c.CompanyName,
(
SELECT TOP 1
ct.CompanyTypeID
FROM CompanyType AS ct
WHERE ct.CompanyID=c....
-1
votes
1
answer
105
views
Entity Framework 6, cannot insert duplicate key violation on update [duplicate]
I have a table ExerciseEnvironments with a unique constraint on a foreign key ExerciseId and type id TypeId combination, where a single exercise can have multiple unique environments but not multiple ...
0
votes
0
answers
33
views
System.NotSupportedException: 'LINQ to Entities does not recognize the method xxx and this method cannot be translated into a store expression
One of the most common filters I apply to a query is to restruct the results to a single (business) organisation e.g. ctx.Users.Where(user => user.OrgID == orgID).
I've created the following ...
0
votes
0
answers
72
views
Entity Framework Core use custom class in LINQ query
We are moving from SQL Server to PostgreSQL. In SQL Server there is a datetimeoffset column type but in PostgreSQL this is not available, so we are splitting it into two columns.
UTC datetime (...
0
votes
0
answers
60
views
Dynamically Combining LINQ Expressions for Entity Framework 4
I am trying to combine multiple LINQ expressions as an OR condition, so that they can be used in Entity Framework 4. The upgrade is not an option here.
The VB code I have is:
Public Function ...
-1
votes
2
answers
170
views
Has EF Core introduced a change in behavior that allows direct entity projection in LINQ queries?
I'm working with EF Core 8 and the MySql provider (Pomelo) in my .NET project. I'm encountering a curious situation where the following code snippet runs without needing Dtos and doesn't produce the ...
0
votes
1
answer
73
views
Query multiple filters from in memory filter object array [duplicate]
I have a filter class structure like this
public class SchemeInvestorDataDateFilter
{
public int SchemeId { get; set; }
public int InvestorId { get; set; }
public DateTime Date { get; set; ...
1
vote
1
answer
40
views
EF6 LINQ query for UTC database timestamps that fall on the same day as a given local timestamp?
I am trying to write an EF6 LINQ query that takes a DateTimeOffset and retrieves all database entries that have a timestamp with the same local date as the given. The database has all timestamps as ...
0
votes
1
answer
313
views
MySql-Provider: Contains in Linq-Query failed
I'm trying to create a Linq query that should work with a Contains on a string list.
The following query works without errors with the SqlServer and PostgreSQL providers:
IEnumerable<string> ...
2
votes
2
answers
596
views
How to run EF Core ExecuteUpdate using multiple Ids using different conditions for each Id
I have this ExecuteUpdate call below inside a for loop and I want to know if it's possible to eliminate the For loop and run ExecuteUpdate as one call?
foreach (var registrant in yogabandEvent....
1
vote
0
answers
78
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.
...
2
votes
1
answer
91
views
How can I run these 2 calls to sum values as one call in EF Core? [duplicate]
Is there a way to make these 2 calls 1? Both are from the same table, just different values.
var grossAmount = await _dbContext.StripeTransfers
.Where(p => p.StripePayoutId == payout....
0
votes
0
answers
84
views
EF Linq query on date
Here's a simplified version of what I'm doing:
DateTime mydate = DateTime.Now;
var myObject = db.Objects.First();
myObject.lastseen = mydate;
db.SaveChanges();
var myobjects = mydb.Objects.Where(a=&...
0
votes
0
answers
77
views
Creating a Lambda for LINQ from code gives error message "Parameter could not be translated"
I am attempting to build a lambda from data.
The module entity has an AlgNo property of type string.
I am trying to get a simple sample code to run before attempting the more advanced case.
Consider ...
0
votes
1
answer
39
views
How to perform nested one-to-many join in LINQ
I apologize if this is stupid, but how to return an IEnumerable<Receipt> that contains Product in this case:
// Each Receipt can have many ReceiptDetals
public class Receipt
{
public int Id {...
1
vote
1
answer
223
views
System.InvalidOperationException: 'The ConnectionString property has not been initialized.' in ASP.NET Core 8, Entity Framework and Linq
I am using ASP.NET Core 8, Entity Framework and Linq to connect to a SQL Server database, but I am getting this error
System.InvalidOperationException: the ConnectionString property has not been ...
0
votes
3
answers
71
views
How to apply group by instead of distinct when need only unique rows?
I am working on ASP.NET Razor page LINQ to SQL function. I face issue I can't display distinct rows from result returned for viewmodel AssetLPOvm
Desired result
If I have 2 or 3 rows as sample exactly ...
1
vote
2
answers
70
views
Use new DateTime in LINQ Queries Where Clause
I have a database here in which, for reasons I cannot understand, the year and the month for the validity of an entry are stored in TWO separate fields...
YearPart=2023
MonthPart=2
Now I need to ...
0
votes
1
answer
250
views
How to check numeric value in linq to entities
I'm trying to translate the following SQL-Select to LINQ to Entities and have a problem with an isnumeric check and hope that the community can give me a hint how to resolve it.
select top(1) column1
...
-1
votes
1
answer
84
views
Compare the int values from enum list
Having this enum :
public enum MyEnums
{
A = 1,
B = 2,
C = 3,
D = 4,
E = 5
}
If I create a list with some enum values from MyEnums for example this :
IEnumerable<MyEnums> ...
2
votes
1
answer
106
views
How add MemberInitExpression into Bindings other Lambda MemberInitExpression
I have the following classes:
class Source {
public int Id { get; set; }
public string Name { get; set; }
public SourceItem Item { get; set; }
}
class SourceItem {
public Guid Id { ...
0
votes
1
answer
78
views
Unable to use simple function in Linq-to-entities query
I have lots of tabular data that needs to be filtered, filtering is supposed to be available for each column in the data. The data is simple types, int, double, string, bool, date. Users specify the ...
4
votes
2
answers
183
views
Weighted Average in LINQ to Entities
Please consider this tables:
Weight:
City Weight
--------------------
Tokyo 100
München 150
Köln 200
and Data:
ID Country City Value
---------...
0
votes
2
answers
93
views
How Can I dynamically create an "OR" LINQ query based on input parameter conditions
My problem is a bit more complicated, so I have simplified it with the below example.
Let's say I have a simple list of Integers. I receive two condition input parameters in my function which tell ...
0
votes
1
answer
101
views
Linq Search by Dictionary value
I am trying to search contacts (Objects) that contains a List<Dictionary<string, string>> by a dictionary key and value
Below is my Json Array of Contacts
[
{
"first_name": ...
0
votes
0
answers
120
views
LINQ-TO-Entities from SQL
I need some help converting below SQL into LINQ
SELECT a.Id FROM
(
SELECT
s.student_id AS Id,
COALESCE(l.city_name, '') AS City,
ROW_NUMBER() OVER (
PARTITION ...
0
votes
1
answer
119
views
Select record with < DateTime returns EQUAL value in EF Core 6
I try to select a date field of two records in a MS SQL Server accounting database table.
The first record's date is selected by id like this:
var lastDate = db.Set<Accounting>().Where(p => p....
0
votes
1
answer
235
views
EF Core 7.0 (PostgresSQL) build IQueryable with RowNumber concept
I have a scenario where one Student (identified by StudentId) has many Courses. Each Course has StudentId(FK) and a Grade (simple text type). I want to build an IQueryable to pull 10 StudentIds (...
0
votes
1
answer
53
views
How to get the grouped result with an EF query for the following data
I have a table Person with firstname, last name, id. Another table with roles(string) for that person, another table with categories(string). these roles and categories are connected with Occupation ...
2
votes
0
answers
61
views
EF6 generates WHERE clause in unexpected order
I'm using the solution from this answer to form an Expression<Func<Proposal, bool>> object (where Proposal is an entity type in my app), according to the searching criteria input on a ...
0
votes
0
answers
164
views
Linq Entity Framework 6.0 SQL query with parameters and constants
I need to build an IQueryable<MyClass> in Linq and then obtain the actual SQL query (to be executed by another actor in my system).
I get to grab the SQL statement using a method extension like
...
0
votes
1
answer
149
views
EF Core Property/Column Sharing
Original Question
I have the following entity I’m trying to get to work with EF Core/NET7, where two properties map to the same column. This way either property could be used in various interfaces ...
0
votes
1
answer
406
views
EF Core CreateDateTime could not be translated
I'm trying to run a linq to entities query in EF Core 6, but I'm receiving an exception saying that System.Data.Entity.DbFunctions.CreateDateTime could not be translated.
My goal is to filter a ...
0
votes
0
answers
65
views
Can I fetch my desired data in one LINQ-to-entities call?
I need a little help in figuring out how I can fetch the location name of the student's home in the below scenario. These are entity framework entities.
class Attendance
{
int Id;
DateTime ...
0
votes
1
answer
390
views
EF 6 foreign key relationship for not compliant naming convention database
I have two tables: Products and Files.
The Products table contains a FK column photo_id that references the Files table.
The Products table can also reference many files (pdf documents), with ...
0
votes
1
answer
41
views
EF Query to change IQueryable<T> to a superclass of T
When using inheritence in Entity Framework, OfType<>() to filter a query to only include rows of the specific subclass. The resulting IQueryable is the type of the subclass. Is there a way to do ...
1
vote
1
answer
50
views
How to filter products in linq to entities and Entity Framework
I have a table that maps Products to Specs and SpecOptions and SpecUnits:
MapProductSpecOptionSpecUnitSpec table:
public class MapProductSpecOptionSpecUnitSpec
{
[Key, Column(Order = 0)]
...
1
vote
0
answers
29
views
Linq to Entities Join on two substrings (one via a navigation property) with a Cross Apply (Method Syntax)
Trying to join two tables on two substrings all while performing a cross apply:
var thisQuery = ctx.Table1
.Join(ctx.Table2,
t1 => t1....
0
votes
2
answers
69
views
How to achieve the same SQL behavior through LINQ-to-entities
I have the below two entity classes in my application:
class Attendance
{
public int Id {get; set;}
public string StudentName {get; set;}
// the linked entity
public SubjectClass SubjectClass ...
0
votes
1
answer
93
views
Filter both master and details record and include details in master
Please consider this scanrio:
I have a Master table:
Id Country
--------------------
1 Ireland
2 Germany
3 Japan
and Detials table:
Id MasterId SellAmount
...
1
vote
2
answers
681
views
How to convert SQL query values separated comma to list by using entity framework core 7?
I work on asp.net core razor page . I face issue How to convert complicated sql query to entity framework core 7
Exactly I need to convert this SQL statement to LINQ to entity without write SQL ...
1
vote
0
answers
58
views
Enfity Framework Core : unable to translate a group by query with a left outer join
I'm completely stuck on this simple query, made with Entity Framework Core 6 that should return a list of questions, counting the number of answers for each of them (it's like a forum...):
questions = ...
2
votes
1
answer
107
views
Linq to Entities and update records
I have the following structure:
public interface IChainByPreviousId
{
int Id { get; set; }
int? PreviousDeviceId { get; set; }
}
public class RegisteredDevice : IChainByPreviousId
{
...
0
votes
2
answers
65
views
Filter model in model records using Entity Framework using C#
I have following classes
class Patient
{
public string fname { get; set; }
public string lname { get; set; }
public IList<Letter> letter { get; set; }
}
class Letter
{
public ...
1
vote
1
answer
91
views
Entity Framework 6 Ignoring Distinct in SQL Query when Selecting Multiple Columns
I am not seeing the expected SQL generated with regard to Distinct when selecting multiple columns. In particular the Distinct is being left out of the generated SQL. Strangely, this seems to work ...
1
vote
1
answer
65
views
How to check for the presence of a substring in a string by removing third-party characters?
I have a SQL Server database. In the orders table, I have a column for phone number. At the moment it contains different values like "+38 (453) 454-35-45", "0506070188", "+23 (...