I have the below code which works but I do not feel this is the best way to achieve the result. I am looking at optimising my code. Any suggestions of a better option will be appreciated. sub is a subcategory which is nullable.
[AllowAnonymous]
public ActionResult _relatedgrps(string cat, string sub)
{
if (!string.IsNullOrWhiteSpace(sub)){
var pgs = db.Pages
.Where(u=>u.MetaNoSearch==false)
.Where(u => u.PaOk == true && u.Category.Name == cat && u.SubCategory.CatName == sub)
.OrderByDescending(u => u.PaCreatedOn);
return PartialView(pgs.ToList());
}else{
var pgs = db.Pages
.Where(u=>u.MetaNoSearch==false)
.Where(u => u.PaOk == true && u.Category.Name == cat )
.OrderByDescending(u => u.PaCreatedOn);
return PartialView(pgs.ToList());
}}
Linq IEnumerables can be additive and the query will only be executed when enumerated for the first time (like calling .ToList()). So you should be able to do something like this:
var pgs = db.Pages
.Where(u => u.MetaNoSearch == false &&
u.PaOk == true &&
u.Category.Name == cat);
if (!string.IsNullOrWhiteSpace(sub))
{
pgs = pgs.Where(u => u.SubCategory.CatName == sub);
}
return PartialView(pgs.OrderByDescending(u => u.PaCreatedOn).ToList());
Create an object to query it. To improve it, you also could remove it boolean comparations because they are conditions.
var query = db.Pages.Where(u => !u.MetaNoSearch && u.PaOk && u.Category.Name == cat);
if (!string.IsNullOrWhiteSpace(sub))
query = query.Where(u => u.SubCategory.CatName == sub);
query = query.OrderByDescending(u => u.PaCreatedOn);
return PartialView(query.ToList());
#user3021830 - be careful with String.IsNullOrWhitespace, you cannot use that in a database query. You could do String.IsNullOrWhitespace(sub), but not String.IsNullOrWhitespace(u.*).
I'd avoid any conditionals in the query because that will likely result in a case statement in the SQL.
To produce the best SQL I'd do something like this:
var pgs = db.Pages.Where(u => u.MetaNoSearch == false)
.Where(u => u.PaOk == true)
.Where(u => u.Category.Name == cat);
if (!string.IsNullOrWhiteSpace(sub))
{
pgs = pgs.Where(u => u.SubCategory.CatName == sub);
}
var result = pgs.OrderByDescending(u => u.PaCreatedOn).ToList();
Related
I'm trying to select several rows from a table but would like the selection to stop if the first where clause comes back with results. Basically if there are results with language then I don't need to query for DevLanguage or FallbackLanguage, is this possible in a single query with || operator or with some other LINQ magic?
var languageStrings = _presentationContext.LocalizationStrings
.Where(x => x.Namespace.Namespace == #namespace &&
x.Namespace.Language == language);
var devStrings = _presentationContext.LocalizationStrings
.Where(x => x.Namespace.Namespace == #namespace &&
x.Namespace.Language == Constants.DevLanguage);
var fallbackStrings = _presentationContext.LocalizationStrings
.Where(x => x.Namespace.Namespace == #namespace &&
x.Namespace.Language == Constants.FallbackLanguage);
var localizationStrings = languageStrings.Any() ? languageStrings : devStrings.Any() ? devStrings : fallbackStrings.Any() ? fallbackStrings
I would put the common part of these queries into a separate query.
var query = _presentationContext.LocalizationStrings.Where(x => x.Namespace.Namespace == #namespace);
var languageStrings = query.Where(x => x.Namespace.Language == language);
var devStrings = query.Where(x => x.Namespace.Language == Constants.DevLanguage);
var fallbackStrings = query.Where(x => x.Namespace.Language == Constants.FallbackLanguage);
The SQL is generated the same.
While there are probably more downsides to the code below than I know of, it's a one-liner (if you ignore the temp variable declaration) and only executes the LINQ queries if the previous one failed to return any results.
IEnumerable<dynamic> temp;
var localizationStrings =
(temp = _presentationContext.LocalizationStrings.Where(x =>
x.Namespace.Namespace == #namespace &&
x.Namespace.Language == language)).Any() ? temp :
(temp = _presentationContext.LocalizationStrings.Where(x =>
x.Namespace.Namespace == #namespace &&
x.Namespace.Language == Constants.DevLanguage)).Any() ? temp :
(temp = _presentationContext.LocalizationStrings.Where(x =>
x.Namespace.Namespace == #namespace &&
x.Namespace.Language == Constants.FallbackLanguage)).Any() ? temp :
Enumerable.Empty<string>();
One reason why I would either pick your original code or search for a different method is because assigning values in a chain like this is not performance friendly.
My requirement is to make boolean value (IsPC=true) only if I found any value with IsCurrent = true from the list and second condition is to filter the list with G or W codes and third condition is to check the PCBNumber length ==15 with only one from the list.
How short can i able to reduce the below query using LINQ method syntax
below is my query
var CurrentQ= p.List.Where(x => x.IsConCurrent== true);
if (CurrentQ.Count() > 0)
{
var NCurrentQwithWorQ = p.List.Where(x => x.Codes == Codes.W|| x.Codes== Codes.Q).Count();
if (NCurrentQwithWorQ != null)
{
var PCBNumber = p.List.Where(x => x.PCBNumber .Length == 15).Count();
if (PCBNumber == 1)
{
isPC = true;
}
}
}
You can use all conditions in same query like below,
var PCBNumber= p.List.Where(x => x.IsConCurrent== true && (x.Codes == Codes.W|| x.Codes== Codes.Q) && x.PCBNumber.Length == 15);
if (PCBNumber !=null && PCBNumber.Count() == 1)
{
isPC = true;
}
I'm not trying to debug what you wrote, but isn't this really what you're looking for--that is, daisy-chaining your Where conditions?
var isPC = p.List.Where(x => x.IsConCurrent == true).Where(x => x.Codes == Codes.W || x.Codes == Codes.Q).Where(x => x.PCBNumber.Length == 15).Count() == 1;
Both solutions suggested above are correct.
p.List.Where(x => x.IsConCurrent== true && (x.Codes == Codes.W|| x.Codes== Codes.Q) && x.PCBNumber.Length == 15);
p.List.Where(x => x.IsConCurrent == true).Where(x => x.Codes == Codes.W || x.Codes == Codes.Q).Where(x => x.PCBNumber.Length == 15).Count()
Actually they are performed in the same way. The Where function does not force immediate iteration through the data source. Only when you execute the Count function, LINQ will process row by row and execute criterion by criterion to find out which values should be calculated.
I can only suggest you add the Take(2) operator after the where clause. In this case LINQ will stop after finding the first two rows that matches provided criterion and other rows will not be processed.
p.List.Where(x => x.IsConCurrent == true)
.Where(x => x.Codes == Codes.W || x.Codes == Codes.Q)
.Where(x => x.PCBNumber.Length == 15)
.Take(2).Count()
I'm trying to write a query to select data from database. I have the following code :
from notes in ctx.Notes
.Where(x => x.UserId== user.UserId
|| x.UserId == user.FamilyId
|| x.UserId == user.CompanyId).DefaultIfEmpty()
The problem with this is that the FamilyId and CompanyId are both nullable types and may not have any value at all which corrupts the whole query. How can I rewrite it so it only looks for FamilyId/CompanyId if they have values?
Create condition query:
var users = ctx.Notes.Where(x => x.UserId == user.UserId);
if (user.FamilyId != null)
{
users = users.Union(ctx.Notes.Where(x => x.UserId == user.FamilyId));
}
if (user.CompanyId != null)
{
users = users.Union(ctx.Notes.Where(x => x.UserId == user.CompanyId ));
}
var result = users.ToArray();
Simple, just add an AND clause to check if it's not null:
from notes in ctx.Notes.Where(x => x.UserId== user.UserId || (user.FamilyId ! =null && x.UserId == user.FamilyId) || (user.CompanyId !=null && x.UserId == user.CompanyId)).DefaultIfEmpty()
I've currently got a working multi level predicate shown below...
var predicate = PredicateBuilder.New<Patient>();
foreach (var code in codeArray) // ect..["AB12", "W231", "Q213"]
{
string localCode = code;
predicate.Or(p => p.Requests.Any(
u => u.Records.Any(
t => t.OutgoingRecords.Any(
s => s.Code == localCode)
)
)
);
}
res = query.AsExpandable().Where(predicate);
// Query is built up from an outside source and is eventually turned into a list via .ToListAsync()
Which is fine except as far as I'm aware it is producing a query in the form of...
query = p => p.PatientEpisodes.Any(u => u.EpisodeGroupings.Any(t => t.EpisodeDiagnoses.Any(s => s.Code == 'AB12'))) ||
p.PatientEpisodes.Any(u => u.EpisodeGroupings.Any(t => t.EpisodeDiagnoses.Any(s => s.Code == 'W231'))) ||
p.PatientEpisodes.Any(u => u.EpisodeGroupings.Any(t => t.EpisodeDiagnoses.Any(s => s.Code == 'Q213')))
Which is just overkill (and somewhat wrong) when all I really should be producing is...
query = p => p.PatientEpisodes.Any(u => u.EpisodeGroupings.Any(t => t.EpisodeDiagnoses.Any(s => s.Code == 'AB12' || s.Code == 'W312' || s.Code == 'Q213')))
Is there a way to insert the predicate at a sub level of a query such as below? My current attempts to do so have failed so far.
var predicate = PredicateBuilder.New<RecordCode>();
foreach (var code in codeArray)
{
string localCode = code;
predicate.Or(p => p.Code == localCode);
}
query = query.AsExpandable().Where(
s => s.Requests.Any(
c => c.Records.Any(
d => d.OutgoingRecords.Any(predicate))));
Many thanks in advance.
My method has a parameter which will determine which column to perform a where filter on, so to make things generic I need to be able to perform some logic on which column I want to perform the where on.
Is it possible to attach a .And clause to a given QueryOver<> query?
public List<..> GetABC(SomeType type)
{
NHibernateHelper.Session.QueryOver<Blah>()
.Where(x => x.name = "")
.And(x => x.a) // if type == SomeType.A then x.a, otherwise x.b (SomeType.B)
}
How could I do this?
I know when doing a criteria query I could create a criteria and then attach it to the query.
Sure.
var query = NHibernateHelper.Session.QueryOver<Blah>()
.Where(x => x.name = "");
if(type == SomeType.A)
{
query = query.And(x => x.a == ...);
}
else
{
query = query.And(x => x.b == ... );
}
The query will only be executed after the ".List()"
Update: I don't know if you're refering to something like this (in your comment)
var query = NHibernateHelper.Session.QueryOver<Blah>()
.Where(x => x.name = "");
Expression<Func<Blah, bool>> typePredicate = null;
if(type == SomeType.A)
{
typePredicate = x => x.a == ...;
}
else
{
typePredicate = x => x.b == ...;
}
query = query.Where(typePredicate);
Or probably you're more interested in something in the likes of Detached queries?
Not sure I completely understand the question ... but something like this?
public List<..> GetABC(SomeType type)
{
NHibernateHelper.Session.QueryOver<Blah>()
.Where(x => x.name = "")
.And(x => typeof(type) == SomeType.A ? x.a : x.b)
}