Once I upgrade my entity framework core to the latest, I cannot execute below query.
enter image description here
var result = db.Actuacion.Where(m => m.ActId == id && m.TempSave != true).Include(m => m.Medico).Include(m=>m.Factura).FirstOrDefault();
Factura and Medico has the same column Name and then EF throw below exception to me
"The column 'PCIVA' was specified multiple times for 't'.
How can I get rid of this
Related
I have a column with a UDT in my table and when I try to query it with EF Core the database throws an error
operator does not exist: example_type = text
This doesn't work:
var type = ExampleType.ExampleValue;
query.Where(e => e.Type == type)...
But if I pass it directly it works
query.Where(e => e.Type == ExampleType.ExampleValue)...
I have an existing linq query which gets some data into a view model object. This is working fine.
I want to add a new property for data from child table which will have column values from a child table in a comma separated string format.
Problem: I am not able to concatenate the results using string.join
Simplified version of tables showing only relevant fields
part
id
part number
1
ABC1
2
DEF1
vendor
id
vendorname
1
acme
2
john
vendor part name (vendor specific part number)
partid
vendorid
partname
1
1
GDSE-553-32
1
2
JWWVV-HH-01
simplified version of query
result = (from p in DBContext.Parts.Where(w => w.EquipmentId == eId)
select new PartModel
{
Id = p.Id,
Number = p.PartNumber,
VendorPartNames= String.Join(",", DBContext.VendorPartName.Where(w => w.PartId == p.Id).Select(s => s.PartName))//this line causes exception (shown below)
});
Exception:
LINQ to Entities does not recognize the method 'System.String Join(System.String, System.String[])' method, and this method cannot be translated into a store expression.
Please note: the actual query has some joins and other columns, so please dont suggest solutions that requires joins.
If I change the "VendorPartName" to a List type , I can get the results without any problems.
My only problem is in "How to convert the results for "VendorPartName" property to a comma separated strings?"
eg: based on sample table data provided, it should be
GDSE-553-32, JWWVV-HH-01
Entity Framework does not support String.Join() method.
So, what we can do is to fetch VendorPartNames as a string collection and then we can later separate it with ,.
Note: For this, we would first use an anonymous object and later convert it to PartModel.
So your query would look like this:
var parts = DBContext.Parts
.Where(w => w.EquipmentId == eId)
.Select(p => new {
Id = p.Id,
Number = p.PartNumber,
VendorPartNames = p.VendorPartName.Select(n => n.PartName)
}).ToList();
var result = parts.Select(i => new PartModel {
Id = i.Id,
Number = i.Number,
VendorPartNames = String.Join(",", i.VendorPartNames)
}).ToList();
So, I have junction table called:
AppointmentsActivities
consisting of:
AppointmentID
ActivityID
I need to implement Update operation. Since it's a junction table the update should be able to not only to update existing records, but also to insert new records, or to delete one who no longer need to exists in the table(because i'm passing an entity with an appointmentID and list of ActivityID's).
I'm struggling to delete the records, that no longer should exist in the table.
I have to delete every record which have the same AppointmentId, but his ActivityID should not be present in any of the objects from the new list of Activities.
The query I have written looks like this :
var remove = _context.AppointmentsActivities.
Where(i => i.AppointmentID == entity.ID && entity.Activities.Any(u => u.ActivityID != i.ActivityID)).
ToList();
Where:
i => i.AppointmentID == entity.ID
Checks if the appointmentID of the newly passed entity is the same as the one in the database table.
And:
entity.Activities.Any(u => u.ActivityID != i.ActivityID)
Is supposed to check if any of the activityID's in the list of Activities equals the activityID from the database table.
Obviosly,I'm missing something, because EF cannot resolve this LINQ query. What am I missing? Any help will be appreciated. Thank you.
Try to rewrite your LINQ query to be acceptable by EF. Any with local collections will not work, so replace with Contains:
var activityIds = entity.Activities.Select(a => a.ActivityID).ToList();
var remove = _context.AppointmentsActivities
.Where(i => i.AppointmentID == entity.ID && !activityIds.Contains(i.ActivityID))
.ToList();
I have an entity query that when I run increments the value of my field. I am using entity framework and sql server 2012. Here is my query;
public void GetLastAccountNumber(ProductLine productLine, Action completed)
{
EntityQuery query = WASMDomainContext.GetContactCustomerAccountsQuery()
.Where(cca => cca.ProductLineId == productLine.Id)
.OrderBy(cca => cca.AccountNumber);
WASMDomainContext.Load(query, loadOp =>
{
Exception error = null;
ContactCustomerAccount lastAccount = null;
if (loadOp.HasError)
error = loadOp.Error;
else
lastAccount = loadOp.Entities.LastOrDefault();
// Invoke completion callback
completed(lastAccount, error);
}, null);
}
Query should return the last account number which is an integer field for now. However it returns an incremented value. For example in my table I have an accountnumber 0 the query returns an entity with account number as 1. My account number field is not auto increament and I find this very strange. And each time the above is called the AccountNumber field value increases by one but the database value will remain 0. I just want the query to return what is in my database. Any idea why this could be happening? Any help will be appreciated. Thank you all.
So I need to get the max value of a column (or the last one) using entity framework and both of these queries throw exceptions:
(The ID I'm trying to retrieve is of type varchar, but it works in raw sql, I think
it should work here too)
This one:
string maxCurrentID = db.reservations.Max().ReservationID;
Throws this:
The specified method 'EntityClub_.reservacion Maxreservacion' on the type 'System.Linq.Queryable' cannot be translated into a LINQ to Entities store expression because no overload matches the passed arguments.
and this one:
string maxCurrentID = db.reservations.LastOrDefault().ReservationID;
LINQ to Entities does not recognize the method 'EntityClub_.reservacion LastOrDefaultreservacion' method, and this method cannot be translated into a store expression.
How can I obtain the expected values?
var maxReservationId=db.reservations.Max(u =>(int?)u.ReservationID) ?? 0;
if there is no data in table it replaces null with 0
You aren't asking for the highest ReservationID, you're trying to get the highest Reservation, and getting its ReservationID. EF does not understand what "the highest Reservation" means.
var maxReservationID = db.reservations.Max(r => r.ReservationID);
or
var maxReservationID = db.reservations.Select(r => r.ReservationID).Max();
should work.