Entity Framework calling MAX on null on Records - entity-framework

When calling Max() on an IQueryable and there are zero records I get the following exception.
The cast to value type 'Int32' failed because the materialized value is null.
Either the result type's generic parameter or the query must use a nullable type.
var version = ctx.Entries
.Where(e => e.Competition.CompetitionId == storeCompetition.CompetitionId)
.Max(e => e.Version);
Now I understand why this happens my question is how is the best way to do this if the table can be empty. The code below works and solves this problem, but its very ugly is there no MaxOrDefault() concept?
int? version = ctx.Entries
.Where(e => e.Competition.CompetitionId == storeCompetition.CompetitionId)
.Select(e => (int?)e.Version)
.Max();

Yes, casting to Nullable of T is the recommended way to deal with the problem in LINQ to Entities queries. Having a MaxOrDefault() method that has the right signature sounds like an interesting idea, but you would simply need an additional version for each method that presents this issue, which wouldn't scale very well.
This is one of many mismatches between how things work in the CLR and how they actually work on a database server. The Max() method’s signature has been defined this way because the result type is expected to be exactly the same as the input type on the CLR. But on a database server the result can be null. For that reason, you need to cast the input (although depending on how you write your query it might be enough to cast the output) to a Nullable of T.
Here is a solution that looks slightly simpler than what you have above:
var version = ctx.Entries
.Where(e => e.Competition.CompetitionId == storeCompetition.CompetitionId)
.Max(e =>(int?)e.Version);

Try this to create a default for your max.
int version = ctx.Entries
.Where(e => e.Competition.CompetitionId == storeCompetition.CompetitionId)
.Max(e =>(int?)e.Version) ?? 0;

You could write a simple extension method like this, it returns the default value of type T if no records exist and is then apply Max to that or the query if records exist.
public static T MaxOrEmpty<T>(this IQueryable<T> query)
{
return query.DefaultIfEmpty().Max();
}
and you could use it like this
maxId = context.Competition.Select(x=>x.CompetitionId).MaxOrEmpty();

I couldnt take no for an answer :) I have tested the below and it works, I havent checked the SQL generated yet so be careful, I will update this once I have tested more.
var test = ctx.Entries
.Where(e => e.Competition.CompetitionId == storeCompetition.CompetitionId)
.MaxOrDefault(x => x.Version);
public static TResult? MaxOrDefault<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
where TResult : struct
{
return source
.Select(selector)
.Cast<TResult?>()
.Max();
}

Try this:
IEnumerable<AlertsResultset> alerts = null;
alerts = (from POA in SDSEntities.Context.SDS_PRODUCT_ORDER_ALERT
join A in SDSEntities.Context.SDS_ALERT on POA.ALERT_ID equals A.ALERT_ID
orderby POA.DATE_ADDED descending
select new AlertsResultset
{
ID = POA.PRODUCT_ORDER_ALERT_ID == null ? 0:POA.PRODUCT_ORDER_ALERT_ID ,
ITEM_ID = POA.ORDER_ID.HasValue ? POA.ORDER_ID.Value : POA.PRODUCT_ID.Value,
Date = POA.DATE_ADDED.Value,
orderType = SDSEntities.Context.SDS_ORDER.Where(o => o.ORDER_ID == POA.ORDER_ID.Value).FirstOrDefault().ORDER_TYPE,
TransactionNumber = POA.PRODUCT_ID.HasValue ? (SDSEntities.Context.SDS_PRODUCT.Where(p => p.PRODUCT_ID == POA.PRODUCT_ID.Value).FirstOrDefault().TRANSACTION_NUMBER) : (SDSEntities.Context.SDS_ORDER.Where(o => o.ORDER_ID == POA.ORDER_ID.Value).FirstOrDefault().TRANSACTION_NUMBER),
Publisher = POA.PRODUCT_ID.HasValue ?
(
SDSEntities.Context.SDS_PRODUCT.Where(p => p.PRODUCT_ID == POA.PRODUCT_ID.Value).FirstOrDefault().PRODUCT_TYPE_NUMBER == "ISSUE" ? (from prod in SDSEntities.Context.SDS_PRODUCT
join ji in SDSEntities.Context.SDS_JOURNAL_ISSUE on prod.PRODUCT_ID equals ji.PRODUCT_ID
join j in SDSEntities.Context.SDS_JOURNAL on ji.JOURNAL_ID equals j.JOURNAL_ID
where prod.PRODUCT_ID == POA.PRODUCT_ID
select new { j.PUBLISHER_NAME }).FirstOrDefault().PUBLISHER_NAME : (from prod in SDSEntities.Context.SDS_PRODUCT
join bi in SDSEntities.Context.SDS_BOOK_INSTANCE on prod.PRODUCT_ID equals bi.PRODUCT_ID
join b in SDSEntities.Context.SDS_BOOK on bi.BOOK_ID equals b.BOOK_ID
where prod.PRODUCT_ID == POA.PRODUCT_ID
select new { b.PUBLISHER_NAME }).FirstOrDefault().PUBLISHER_NAME
)
: (SDSEntities.Context.SDS_ORDER.Where(o => o.ORDER_ID == POA.ORDER_ID.Value).FirstOrDefault().PUBLISHER_NAME),
Alert = A.ALERT_NAME,
AlertType = A.ALERT_TYPE,
IsFlagged = POA.IS_FLAGGED.Value,
Status = POA.ALERT_STATUS
});

how about
var version = ctx.Entries
.Where(e => e.Competition.CompetitionId == storeCompetition.CompetitionId)
.Max(e => (int?)e.Version);
less ugly, more elegant

I want to suggest a merge from the existing answers:
#divega answer works great and the sql output is fine but because of 'don't repeat yourself'
an extension will be a better way like
#Code Uniquely showed. But this solution can output more complex sql as you needed.
But you can use the following extension to bring both together:
public static int MaxOrZero<TSource>(this IQueryable<TSource> source,
Expression<Func<TSource, int>> selector)
{
var converted = Expression.Convert(selector.Body, typeof(int?));
var typed = Expression.Lambda<Func<TSource, int?>>(converted, selector.Parameters);
return source.Max(typed) ?? 0;
}

You can use:
FromSqlRaw("Select ifnull(max(columnname),0) as Value from tableName");

Related

EF Core 5.0 - The linq expression could not be translated. either rewrite the query

This question has been asked before and answered a few times, but not in a generalized way. Rather, the answers are specific to the asker (which makes sense)...but I use this pattern a lot, and I'm sure others do as well, so I'm looking for a more general answer. I know why this is happening, but I'm not sure what to do about it exactly. the offending line below is
RoleId = ur.First(xu => xu.UserId == x.Id).RoleId,
My understanding is that I have to convert this in some way before the projection, but that's where I'm stuck. Or use Contains() instead of First() (which is an implicit Where() filter), but I'm not sure how to rewrite it so the server will do the query, instead of the client. What is the best way forward?
var roles = await _roleManager.Roles.ToListAsync();
var rolesList = roles.Select(x => new {x.Id, x.Name}).ToList();
var rid = ur.First(xu => xu.UserId == 4).RoleId;
var ur = await _context.UserRoles.ToListAsync();
var appUsers = await _context.Users
.Select(x => new AppUserViewModel
{
Id = x.Id,
StaffId = x.StaffId,
Email = x.Email,
UserName = x.UserName,
PhoneNumber = x.PhoneNumber,
RoleId = ur.First(xu => xu.UserId == x.Id).RoleId,
RoleSelectListItems = rolesList.Select(yy => new SelectListItem
{
Value = yy.Id.ToString(),
Text = yy.Name
}).ToList()
})
.ToListAsync();
Actually you should work with IQueryable, not lists.
var roles = _roleManager.Roles.AsQueryable();
var ur = _context.UserRoles.AsQueryable();
var appUsers = await _context.Users
.Select(x => new AppUserViewModel
{
Id = x.Id,
StaffId = x.StaffId,
Email = x.Email,
UserName = x.UserName,
PhoneNumber = x.PhoneNumber,
RoleId = ur.FirstOrDefault(xu => xu.UserId == x.Id).RoleId,
RoleSelectListItems = roles.Select(yy => new SelectListItem
{
Value = yy.Id.ToString(),
Text = yy.Name
}).ToList()
})
.ToListAsync();
I got an answer to this. Linq sends itself down to the database, but certain expressions can't be interpreted by the translator which is why it throws this error. So if, for example, your Linq expression references a method to return a value, well, that method can't be passed down to your db, so you have to get that value before you use it in your Linq expression.
So in the example above, RoleId = ur.First(xu => xu.UserId == x.Id).RoleId, it's really two expressions, and the translator doesn't know what to do with that. The first part is "return to me the first object in the userRoles list where the id equals the id of the user" and the second part is "and now give me the RoleId". But you can't send that down - it can't be translated. So I'd have to get the RoleId from the userRoles list as a separate pair of transactions.
Get the role object, then separate out the RoleId value from that object.

How to sort on DB side, if entities are not connected via Navigation (since it's not possible)

I want to have my EfCore query translated into the following SQL query:
select
c.blablabla
from
codes c
left join lookups l on c.codeId = l.entityid and l.languageCode = <variable - language code of current thread> and l.lookuptype = 'CODE'
where
..something..
order by
l.displayname
Note: tables 'codes' and 'lookups' are not connected! 'lookups' contains a lot of different lookup data in different languages!
I am stuck into limitations of EfCore (like 'NavigationExpandingExpressionVisitor' failed). I don't want to make in-memory filtering, it looks silly to me... Am I missing something obvious?
In perspective, I'd like to make universal method to help sort by displayname (or other lookup name) for different kind of entities - not only codes.
Seems like I figured it out. If there's a better approach - please let me know:
protected override IQueryable<FixCode> SortByDisplayName(IQueryable<FixCode> queryable, string languageCode = null)
{
return queryable
.GroupJoin(
DbContext.FixCodeValues.Where(x =>
x.DomainId == CentralToolConsts.Domains.CENTRAL_TOOLS
&& x.CodeName == CentralToolsFieldTypes.CODE_ORIGIN
&& (x.LanguageCode == languageCode || x.LanguageCode == CentralToolsDbLanguageCodes.English)),
//TODO: this will be a 'selector' parameter
code => code.CodeOriginId,
codeOrigin => codeOrigin.StringValue,
(c, co) => new
{
Code = c,
CodeOrigin = co
}
)
.SelectMany(
x => x.CodeOrigin.DefaultIfEmpty(),
(x, codeOrigin) => new { Code = x.Code, CodeOrigin = codeOrigin }
)
.OrderBy(x => x.CodeOrigin.ShortName)
.Select(x => x.Code);
}

Is it possible to combine multiple IQueryable into a single one without using Union?

Good evening. Is it possible to combine multiple IQueryable into a single one without using Union in EF Core 2? The main problem is that it doesn't allow to use methods like .Union(), .Except() etc.
It's important that IQueryable should be executed as a single query.
This is what I want to get: toCreateSubquery.Union(toDeleteSubquery)
The queries I want to combine are listed below.
var toCreateSubquery =
validLinks
.Where(
rl => !existingLinks.Any(
el => el.Contract == rl.Contract && el.Estate == rl.Estate)) // Except() doesn't work'
.Select(x => new {x.Contract, x.Estate, Type = ActionType.Create});
var toDeleteSubquery =
existingLinks
.Where(el => !validLinks.Any(rl => el.Contract == rl.Contract && el.Estate == rl.Estate))
.Select(x => new {x.Contract, x.Estate, Type = ActionType.Delete});
This is the visualization of the problem I'm solving.
I want to get the union of these sets without intersection and be able to distinguish belonging to one of these sets.
Just in case, I attach the code of getting these sets:
var validLinks = from ac in _context.AreaContracts
from e in _context.Estate
where (from al in e.AreaLinks select al.Area.Id).Contains(ac.Area.Id) ||
e.Geometry.Intersects(
ac.Area.Geometry)
select new { Contract = ac.Contract.Id, Estate = e.Id };
var existingLinks = from sd in _context.SquareDistributions
select new { Contract = sd.Contract.Id, Estate = sd.Estate.Id };
Thank you for your attention.

Adding Data to a very nested child object using EntityFramework

I am trying to add a note to my event object. I am getting an error using this code
Note noteToAdd = new Note { State = State.Added, NoteText = note };
Patient patient = context.Patients.Find(patientId);
patient.State = State.Modified;
patient.MobilePatient.State = State.Modified;
patient.MobilePatient.MCalmEvents.Find(e => e.Id == eventid).Note = noteToAdd;
context.ApplyStateChanges();
Is there a better way to do it using Linq To Entity?
The error that I am having is :
{"Invalid column name 'Note_Id'."}
and the SQl that is being generated is a SELECT instead of INSERT.
Thank you
but your map shows a one-to-many relation between Note and Event...
all of your code remain as they are, but instead of this line :
patient.MobilePatient.MCalmEvents.Find(e => e.Id == eventid).Note = noteToAdd;
replace these lines:
noteToAdd.EventID = oEvent.ID; // replace field names, to exactly what they are;
context.Note.Add(noteToAdd);
var oEvent = patient.MobilePatient.MCalmEvents.Find(e => e.Id == eventid);
oEvent.NoteID = noteToAdd.ID; // replace field names, to exactly what they are;
also i think if you don`t write these two:
var oEvent = patient.MobilePatient.MCalmEvents.Find(e => e.Id == eventid);
oEvent.NoteID = noteToAdd.ID; // replace field names, to exactly what they are;
there is not any problem, i`m not sure
According to your map, Event entity has a list of Note as navigation property, and i think you should add to this collection instead, what you write in this line:
patient.MobilePatient.MCalmEvents.Find(e => e.Id == eventid).Note = noteToAdd;
i think should be like this:
patient.MobilePatient.MCalmEvents.Find(e => e.Id == eventid).Add(noteToAdd);
in addition, what kind of error you get ? can you explain your error ?
are sure there is no add method on Event navigation property
why don`t you try to add note from context directly? like:
context.Note.Add(noteToAdd);

EF DbContext. How to avoid caching?

Spent a lot of time, but still cann't understand how to avoid caching in DbContext.
I attached below entity model of some easy case to demonstrate what I mean.
The problem is that dbcontext caching results. For example, I have next code for querying data from my database:
using (TestContext ctx = new TestContext())
{
var res = (from b in ctx.Buildings.Where(x => x.ID == 1)
select new
{
b,
flats = from f in b.Flats
select new
{
f,
people = from p in f.People
where p.Archived == false
select p
}
}).AsEnumerable().Select(x => x.b).Single();
}
In this case, everything is fine: I got what I want (Only persons with Archived == false).
But if I add another query after it, for example, query for buildings that have people that have Archived flag set to true, I have next things, that I really cann't understand:
my previous result, that is res, will be added by data (there
will be added Persons with Archived == true too)
new result will contain absolutely all Person's, no matter what Archived equals
the code of this query is next:
using (TestContext ctx = new TestContext())
{
var res = (from b in ctx.Buildings.Where(x => x.ID == 1)
select new
{
b,
flats = from f in b.Flats
select new
{
f,
people = from p in f.People
where p.Archived == false
select p
}
}).AsEnumerable().Select(x => x.b).Single();
var newResult = (from b in ctx.Buildings.Where(x => x.ID == 1)
select new
{
b,
flats = from f in b.Flats
select new
{
f,
people = from p in f.People
where p.Archived == true
select p
}
}).AsEnumerable().Select(x => x.b).Single();
}
By the way, I set LazyLoadingEnabled to false in constructor of TestContext.
Does anybody know how to workaround this problem? How can I have in my query what I really write in my linq to entity?
P.S. #Ladislav may be you can help?
You can use the AsNoTracking method on your query.
var res = (from b in ctx.Buildings.Where(x => x.ID == 1)
select new
{
b,
flats = from f in b.Flats
select new
{
f,
people = from p in f.People
where p.Archived == false
select p
}
}).AsNoTracking().AsEnumerabe().Select(x => x.b).Single();
I also want to note that your AsEnumerable is probably doing more harm than good. If you remove it, the Select(x => x.b) will be translated to SQL. As is, you are selecting everything, then throwing away everything but x.b in memory.
have you tried something like:
ctx.Persons.Where(x => x.Flat.Building.Id == 1 && x.Archived == false);
===== EDIT =====
In this case I think you approach is, imho, really hazardous. Indeed you works on the data loaded by EF to interpret your query rather than on data resulting of the interpretation of your query. If one day EF changes is loading policy (for example with a predictive pre-loading) your approach will "send you in then wall".
For your goal, you will have to eager load the data you need to build your "filterd" entity. That is select the building, then foreach Flat select the non archived persons.
Another solution is to use too separate contexts in an "UnitOfWork" like design.