LINQ to Entities does not recognize the method - subquery count - c#

Hi this question has been asked before but I'm struggling to find an answer that matches my question (the error comes up a lot).
This looks to be similar but with 4 years ago has there been any progress.
LINQ to Entities does not recognize the method 'Int32
Basically I want to move a subquery that returns a count into a function anyway here's the code.
vm.SicknessEpisodes = _db.SicknessEpisodes.Where(x => x.Status == true && x.LastDay == null).OrderByDescending(x => x.FirstDay).
Select(x => new HomeSicknessEpisode
{
SicknessEpisode = x,
EpisodesIn12Months = _db.SicknessEpisodes.Where(y => y.StaffID == x.StaffID && y.Status == true &&
(y.LastDay == null || (y.LastDay.HasValue && y.LastDay >= prevDate))).Count()
}).
ToPagedList(p, 100);
This works
This
vm.SicknessEpisodes = _db.SicknessEpisodes.Where(x => x.Status == true && x.LastDay == null).OrderByDescending(x => x.FirstDay).
Select(x => new HomeSicknessEpisode
{
SicknessEpisode = x,
EpisodesIn12Months = _db.episodes12Months(x,prevDate).Count()
}).
ToPagedList(p, 100);
public IQueryable<SicknessEpisode> episodes12Months(SicknessEpisode x, DateTime prevDate)
{
return this.SicknessEpisodes.Where(y => y.StaffID == x.StaffID && y.Status == true &&
(y.LastDay == null || (y.LastDay.HasValue && y.LastDay >= prevDate)));
}
Does not.
Can anyone tell me how I can make it work - ideally without going into a List and then foreach each episode to run the count.
I'm just trying to make it as efficient as possible for Entity Framework.
If it's not possible - then that's fine - one of those things.

Related

how to conditionally add where condition in linq [duplicate]

This question already has answers here:
Linq dynamically adding where conditions
(3 answers)
Closed 2 years ago.
I am using entity framework and i want to add a where condition only if the condition is met else i dont want that where to be executed which i want to do it in 1 query
Context.Test
.Where(v => v.Valuation.ValuationPeriod.PeriodName == periodName && parameters.SelectedTemplateIds.Contains(v.TemplateTypeId) && fundBusinessIds.Contains(v.Fund.BusinessId))
.Where(v => parameters.sortedList.Contains(v.Valuation.PortfolioCompany.DealCode))
.Where(v => wpGroup == null || v.Valuation.PortfolioCompany.WPGroup == wpGroup)
.Where(v => !myDeals || v.Valuation.PortfolioCompany.UserPermission.Any(up => up.UserId == userId))
so in the above query
Where(v => parameters.sortedList.Contains(v.Valuation.PortfolioCompany.DealCode))
I want to execute only if
parameters.sortedList.Count() > 0
else i dont want to include that where condition in the above query. So basically here i know we can do by seperating the query that is first get a list of values and then check this condition and if it matches then add a where on that list. But is it possible without doing that?
The filter could be added contitionally as follows by using an intermediate assignment.
var result = Context.Test.Where(v => v.Valuation.ValuationPeriod.PeriodName == periodName && parameters.SelectedTemplateIds.Contains(v.TemplateTypeId) && fundBusinessIds.Contains(v.Fund.BusinessId));
if (parameters.sortedList.Count() > 0)
{
result = result.Where(v => parameters.sortedList.Contains(v.Valuation.PortfolioCompany.DealCode));
}
result = result.Where(v => wpGroup == null || v.Valuation.PortfolioCompany.WPGroup == wpGroup)
.Where(v => !myDeals || v.Valuation.PortfolioCompany.UserPermission.Any(up => up.UserId == userId));
You can use this
.Where(v => parameters.sortedList.Count() > 0 ?
parameters.sortedList.Contains(v.Valuation.PortfolioCompany.DealCode) : true)
LINQ queries don't execute unless you do something that causes enumeration of the resulting IEnumerable, which means you can have a pattern of:
var x = collection.Where(...);
if(sometest)
x = x.Where(..);
var result = x.ToList(); //enumerate, causing evaluation
If sometest is true then both the Where will apply (AND style; both where predicates would have to result in a true for a collection entity to appear in the result), if sometest is false, then only the first Where applies
You can add an extension
public static class WhereIfExtension
{
public static IEnumerable<TCollection> WhereIf<TCollection>(this IEnumerable<TCollection> source,
bool condition, Func<TCollection, bool> predicate)
{
return condition ? source.Where(predicate) : source;
}
public static IQueryable<TCollection> WhereIf<TCollection>(this IQueryable<TCollection> source, bool condition,
Expression<Func<TCollection, bool>> predicate)
{
return condition ? source.Where(predicate) : source;
}
}
then
Context.Test
.Where(v => v.Valuation.ValuationPeriod.PeriodName == periodName && parameters.SelectedTemplateIds.Contains(v.TemplateTypeId) && fundBusinessIds.Contains(v.Fund.BusinessId))
.WhereIf(parameters => parameters.sortedList.Count() > 0, parameters.sortedList.Contains(v.Valuation.PortfolioCompany.DealCode))
.Where(v => wpGroup == null || v.Valuation.PortfolioCompany.WPGroup == wpGroup)
.Where(v => !myDeals || v.Valuation.PortfolioCompany.UserPermission.Any(up => up.UserId == userId))

Linq To Entities Internal Conversion

I must to execute this expression:
var CAP = modelCap.AZCPC00F.Where(x => x.CPCVER == CPCVER && x.CPCNAR == CPCNAR && x.CPCCAP == CPCCAP)
.Where(x => XXXXKG == 0 ? true : Convert.ToInt64(Convert.ToDouble(x.CPCLKG)) < XXXXKG)
.Where(x => XXXXMC == 0 ? true : Convert.ToInt64(Convert.ToDouble(x.CPCLMC)) < XXXXMC)
.Where(x => XXXXFD == "N" ? true : x.CPCZFD == XXXXFD).FirstOrDefault();
When I try to execute this I have an exception on the internal conversion of x.CPCLKG. The exception is:
LINQ to Entities does not recognize the method 'int64' method, and this method cannot be translated into a store expression.
I know that the problem is in the conversion but how can i use this function?
An example of x.CPCLKG is 9.9999 and is an nChar character type.
Thanks to all
You need to have the logic that is not supported in Linq To Entities done in an in-memory query. We can do this by calling ToList() on the parts of the query that are supported. This will execute those parts, and return the results as a list. We can then execute whatever Linq supports on that list, in memory.
var CAP = modelCap.AZCPC00F.Where(x => x.CPCVER == CPCVER && x.CPCNAR == CPCNAR && x.CPCCAP == CPCCAP)
.Where(x => XXXXFD == "N" ? true : x.CPCZFD == XXXXFD)
.ToList();
var CAP2 = CAP.Where(x => XXXXKG == 0 ? true : Convert.ToInt64(Convert.ToDouble(x.CPCLKG)) < XXXXKG)
.Where(x => XXXXMC == 0 ? true : Convert.ToInt64(Convert.ToDouble(x.CPCLMC)) < XXXXMC).FirstOrDefault()

filtering results with Entity Framework

another kinda newbie question I guess. I have EF setup and now I want to select some records based on a filter. I have SomeClass with 4 items (all strings to keep things simple, lets call them string1, string2, and so on). Now, in a post I send the filter in an instance of SomeClass, but maybe not all properties are filled in.
So you might end up with string1="something", string2="bla" and string4="bla2". So string 3 = null. Now, how do I setup the query? If i try something like:
var dataset = entities.mydatabase
.Where(x => x.string1 == someclass.string1 && x.string2 == someclass.string2 && x.string3 == someclass.string3 && x.string4 == someclass.string4)
.Select(x => new { x.string1, x.string2, x.string3, x.string4}).ToList();
... I get no results, because string3=null. I could do something with checking all parameters and see if they're set and create the query based on that, but there must be something more elegant than that.
Anyone?
Thanks!
Ronald
The following will return all rows where the someclass.string is null OR equals to x.string.
var dataset = entities.mydatabase
.Where(x => someclass.string1 == null || x.string1 == someclass.string1)
.Where(x => someclass.string2 == null || x.string2 == someclass.string2)
.Where(x => someclass.string3 == null || x.string3 == someclass.string3)
.Where(x => someclass.string4 == null || x.string4 == someclass.string4)
.Select(x => new { x.string1, x.string2, x.string3, x.string4}).ToList();

LINQ to Entities does not recognize the method 'System.Linq.IQueryable in nested select

I wrote an extension method to get only approved absences out of a list of absences:
public static IQueryable<tblAbwesenheit> OnlyApprovedAbsences(this IQueryable<tblAbwesenheit> source)
{
return source.Where(a =>
(a.tblAbwesenheitsantraggenehmigungs.Any() && a.tblAbwesenheitsantraggenehmigungs.All(g => g.AbwesenheitsgenehmigungsstatusID == AbsenceStatusIds.Approved))
&& (!a.tblAbwesenheitsstornierunggenehmigungs.Any() || a.tblAbwesenheitsstornierunggenehmigungs.Any(g => g.AbwesenheitsgenehmigungsstatusID != AbsenceStatusIds.Approved)));
}
When I'm using this method with a "normal" Select, everything is fine:
context.tblAbwesenheits.OnlyApprovedAbsences().ToList()
However when I'm using it inside a Select statement, I get an error:
context.tblMitarbeiters.Select(m => new
{
Employee = m,
AbsencesForEmployee = m.tblAbwesenheits.OnlyApprovedAbsences()
})
.ToList();
LINQ to Entities does not recognize the method
'System.Linq.IQueryable1[Data.tblAbwesenheit]
OnlyApprovedAbsences(System.Linq.IQueryable1[Data.tblAbwesenheit])'
method, and this method cannot be translated into a store expression.
I have searched quite a lot, but could not find a way to teach Entity Framework to recognize my Method without expanding the query to
context.tblMitarbeiters.Select(m => new
{
Employee = m,
AbsencesForEmployee = m.tblAbwesenheits
.Where(a =>
(a.tblAbwesenheitsantraggenehmigungs.Any() && a.tblAbwesenheitsantraggenehmigungs.All(g => g.AbwesenheitsgenehmigungsstatusID == AbsenceStatusIds.Approved))
&& (!a.tblAbwesenheitsstornierunggenehmigungs.Any() || a.tblAbwesenheitsstornierunggenehmigungs.Any(g => g.AbwesenheitsgenehmigungsstatusID != AbsenceStatusIds.Approved)))
})
.ToList();
Is there a way to get EF to recognize my Method?
EF is trying to look for a SQL equivalent of your method and not finding one. It can find an equivalent of the expanded query, which is why that works.
You might be able to create an expression rather than a method
var OnlyApprovedAbsencesExpression = (a =>
(a.tblAbwesenheitsantraggenehmigungs.Any() && a.tblAbwesenheitsantraggenehmigungs.All(g => g.AbwesenheitsgenehmigungsstatusID == AbsenceStatusIds.Approved))
&& (!a.tblAbwesenheitsstornierunggenehmigungs.Any() || a.tblAbwesenheitsstornierunggenehmigungs.Any(g => g.AbwesenheitsgenehmigungsstatusID != AbsenceStatusIds.Approved)))
and then write something like
AbsencesForEmployee = m.tblAbwesenheits.Where(OnlyApprovedAbsencesExpression)

How to create a LINQ request

I can't create a LINQ request.
I have a base request:
var result = from i in _dbContext.Users
where i.ID != CurrentUserID &&
//i.UserType.UserTypeID == (from a in _dbContext.UserTypes where a.UsersSelectMeetingCriteria.Any(p => p.ID == CurrentUserID) select a.UserTypeID).FirstOrDefault() &&
i.Services.Any(p => p.UsersSelectMeetingCriteria.Any(k => k.ID == CurrentUserID)) &&
i.GeographicalAreas.Any(p=>p.UsersSelectMeetingCriteria.Any(o=>o.ID == CurrentUserID)) &&
i.MultiplyItems.Any(r => (r.UsersSelectMeetingCriteria.Any(q => q.ID == CurrentUserID) && r.ItemType == MultiplyItemKeys.USER_TYPE)) &&
i.MultiplyItems.Any(s => (s.UsersSelectMeetingCriteria.Any(q => q.ID == CurrentUserID) && s.ItemType == MultiplyItemKeys.COMPANY_INVOLVED)
)
select new DataTable.UserModel()
{ ... };
But I need 4 level search. If all 4 where is ok then this is first level of users, if only 3 (but 4th is NOT) - level #2, if 2 - yes, 2 - no then level #3, if only one match - level #4. How to do it?
Untested, and a rather wild stab in the dark, but maybe this will point you in the right direction:
var result = from i in _dbContext.Users
let check1 = i.Services.Any(p => p.UsersSelectMeetingCriteria.Any(k => k.ID == CurrentUserID))
let check2 = i.GeographicalAreas.Any(p=>p.UsersSelectMeetingCriteria.Any(o=>o.ID == CurrentUserID))
let check3 = i.MultiplyItems.Any(r => (r.UsersSelectMeetingCriteria.Any(q => q.ID == CurrentUserID) && r.ItemType == MultiplyItemKeys.USER_TYPE))
let check4 = i.MultiplyItems.Any(s => (s.UsersSelectMeetingCriteria.Any(q => q.ID == CurrentUserID) && s.ItemType == MultiplyItemKeys.COMPANY_INVOLVED))
let level = 5 - (check1 ? 1 : 0) - (check2 ? 1 : 0) - (check3 ? 1 : 0) - (check4 ? 1 : 0)
where i.ID != CurrentUserID && level <= 4
select new {i, level};
What this does is performs your checks independently then subtracts the number of succeeded checks from 5. Thus if three succeed and one fails, you will have 5-3 = "level 2".
Note, if I am actually on the right track here, this looks like a giant mess and an even bigger hack. I'd be inclined move the logic to a stored procedure or try to simplify it somewhere else in the data model.
Note 2, I've also dutifully ignored your commented code.
Note 3, I'm sure there is a better way to do this if I were to know your data model and reasoning behind the query.

Categories

Resources