I am currently doing some refactor on code that makes my application very slow. I am pretty far but i am still missing some pieces of the puzzle, i hope you can help me.
I like to reuse some Linq to SQL code inside of my project. This is my way of doing it at this moment:
public DomainAccount GetStandardUserAccount()
{
return this.DomainAccounts.Where(da => da.DomainAccountType == DomainAccountType.Standarduser).First() as DomainAccount;
}
var CurrentSituation = _context.Employees.ToList().Where(e => e.GetStandardUserAccount().Username.Contains("test")).ToList();
A small clarification: Every employee has multiple domain accounts where one always is a standarduser(DomainAccountType) domainaccount.
Because Linq can not convert an C# methode to an sqlstatement (Eventho its linq to sql code only) I have to convert the dbset to a list first so i can use the GetStandardUserAccount(). This code is is slow because of this whole dbset conversion. Is there a way i can reuse linq to sql code without turning it in an methode? I have read some threads and this is what I got untill now:
Func<Employee, DomainAccount> GetStandardDomainAccount = x => x.DomainAccounts.FirstOrDefault(d => d.DomainAccountType == DomainAccountType.Standarduser);
var TheGoal = _context.Employees.Where(e => e.GetStandardDomainAccount().Username.Contains("Something")).ToList();
Func, not anExpression<Func>. Anyway you have to call that delegate, either by callingInvokeor just()._contextvariable), then at least you have a problem with the query:string.Containsresults intoLIKE '%whatever%', which, in turn, does not use indexes. 2)aswithout following null-checking is a way to NRE. If something must beDomainAccount, then cast it explicitly. If it can beDomainAccount, and its OK, when it is notDomainAccount, then useaswith following null-checking.