Here is my code:
Notification.all.findAll{it.actionTeamBy != null && it.user.id == params?.getLong('user_id') && it.status == true}
Thank in advance!
You are looking for standard Iterable sorting that is done after the findAll call.
assert [3,4,2,7,4].sort() == [2, 3, 4, 4, 7]
So that would translate into something like this
Notification.all.findAll {
it.actionTeamBy != null && it.status == true &&
it.user.id == params?.getLong('user_id')
}.sort { a, b ->
// implement sorting mechanism here
// b.id <=> a.id // could work for you
}
Related
I want to check if columns A B and C are filled with 'Yes' or 'yes' if so a action will follow.
I have this piece of code, and this is working for capitalized 'Yes'
if([1,2,3].indexOf(e.range.getColumn()) > -1) {
if (Col1 == "Yes" && Col2 == "Yes" && Col3 == "Yes") {Some action here}
But when I add || (OR) to it, the code fails.
if([1,2,3].indexOf(e.range.getColumn()) > -1) {
if ((Col1 == "Yes"||"yes") && (Col2 == "Yes"||"yes") && (Col3 == "Yes"||"yes") ) {Some action here}
Hoping for some help here ;)
If I understand the requirement the current approach may be cumbersome. A conditional count (case insensitive) of the three columns looking for "Yes" and then further action conditional on the result being 3 might be a little simpler.
Your OR statement is not right - you need to specify the variable that you are checking again. For instance:
if([1,2,3].indexOf(e.range.getColumn()) > -1) {
if ((Col1 == "Yes" || Col1 == "yes") && (Col2 == "Yes" || Col2 == "yes") && (Col3 == "Yes" || Col3 == "yes") ) {Some action here}
Just a simple question here
I uses the following WHERE query
def instance = ClassName.where{varone == 'A' &&
vartwo == 'B' && varthree == 'C'}.list()
And it returned me the object that i wanted --> ClassName(unsaved)
But when i tried to do the following
def instance2 = ClassName.where{varone == params.varone &&
vartwo == params.vartwo && varthree == params.varthree}.list()
It returned me the following which i couldn't do anything on it --->
grails.gorm.DetachedCriteria#somenumbershere
I don't understand what are the differences between these two. I need the 2nd query to return the same object as what the first query returned.
I have a List generated from Linq to Entities query. In which, I need to get a unique records based on BibId. I have tried changing the query but no help to get the unique records based on BibId.
Query
aa.NewBibContentsModel = (from x in db.BibContents
where (x.TagNo == "245" && x.NormValue == aa.CurrentTitle) || (x.TagNo == "020" && x.NormValue == aa.CurrentISBN) || (x.TagNo == "022" && x.NormValue == aa.CurrentISBN)
select new
{
BibId = x.BibId,
Title = (from a in db.BibContents where a.BibId == x.BibId && a.TagNo == "245" orderby a.Id ascending select a.NormValue),
//Tit = (from a in db.BibContents where a.BibId == line.BibId && a.TagNo == "245" && a.Sfld == "a" select a.NormValue).FirstOrDefault(),
Author = (from a in db.BibContents where a.BibId == x.BibId && splitted.Contains(a.TagNo) && a.NormValue != null select a.TagNo).FirstOrDefault(),
ISBN = (from a in db.BibContents where a.BibId == x.BibId && a.NormValue != null && (a.TagNo == "020" || a.TagNo == "022") orderby a.Id ascending select a.NormValue)
}).AsEnumerable().Select(x => new BibContentsModel
{
BibId = x.BibId,
Title = string.Join(" ", x.Title),
Author = string.Join(" ", (from a in db.BibContents where a.BibId == x.BibId && a.TagNo == x.Author orderby a.Id select a.NormValue)),
ISBN = string.Join(" ", x.ISBN)
}).ToList();
Any help to this problem will be appreciated.
Thanks
What you're trying to achieve is know as Distinct By. MoreLinq has a function for it. The syntax would look like:
(from x in db.BibContentsNo == "022")
... // your query
}).AsEnumerable()
.DistinctBy(x => x.BibId) // <= MoreLinq
What is does is group the records by BibId and take the first element of each group.
You can download MoreLinq as a NuGet package.
I need my LINQ Query to return the Product Datatype, after being grouped. It seems to be encased into an anonymous psuedo family.
I have some properties in Product that I don't care about, just needing the p.ID and p.Name etc.
The error I'm getting at the moment with this is:
The entity or complex type 'DatabaseModel.Product' cannot be constructed in a LINQ to Entities query.
This is my Method:
public static List<Product> GetSellableSpecialOffers(DatabaseEntities db)
{
var query = from p in db.Products
where (p.Visible == true && p.Active == true)
&& p.ShowInSpecialOffers == true
group p by p.FamilyID == null ? 0 : p.FamilyID into f
select new Product {
ID = f.First().ID,
Name = f.First().Name,
Cost = f.First().Cost,
RRP = f.First().RRP
};
return query.ToList();
}
What is the problem? Is there a better way around this? SQL would always return 1 record instead of encasing the object in a secondary datatype, I don't get it.
Many thanks,
EDIT 1:
My apologies for extending the specification, but I need the returned product to be programatically generated e.g.
select new Product {
ID = f.First().ID,
Name = f.First().Name,
Cost = f.OrderBy(p => p.NowCost).FirstOrDefault(),
RRP = f.First().RRP
}
or if I could strongly type the family class:
public partial class ProductFamily
{
public Product GroupedProduct
{
get
{
return this.Products.OrderBy(p => p.NowCost).FirstOrDefault();
}
}
}
Then I would do:
var query = (from p in db.Products
where (p.Visible == true && p.Active == true)
&& p.ShowInSpecialOffers == true
group p by p.ProductFamily == null ? null : p.ProductFamily into f
select f.GroupedProduct).ToList<Product>();
But I can't get either solution to work with what I have.
You can try (boring, but not sure you have the choice)
var query = from p in db.Products
where (p.Visible == true && p.Active == true)
&& p.ShowInSpecialOffers == true
group p by p.FamilyID == null ? 0 : p.FamilyID into f
select new {
ID = f.First().ID,
Name = f.First().Name,
Cost = f.OrderBy(m => m.NowCost).First().Cost,
RRP = f.First().RRP
};
return query.ToList().Select(m =>
new Product {
ID = m.ID,
Name = m.Name,
Cost = m.Cost,
RRP = m.RRP
};
EDIT
Or as pointed by Master Skeet (not exactly the same as what you tried, but much easier)
var query = from p in db.Products
where (p.Visible == true && p.Active == true)
&& p.ShowInSpecialOffers == true
group p by p.FamilyID == null ? 0 : p.FamilyID into f
select f.OrderBy(m => m.NowCost).First();
You could simply select f.First() and return the resulting data, like this:
var query = from p in db.Products
where (p.Visible == true && p.Active == true)
&& p.ShowInSpecialOffers == true
group p by p.FamilyID == null ? 0 : p.FamilyID into f
select f.First();
Then outside the method use whatever properties you need, ignoring what you don't care about (you are handling Product objects, properties should be there even if you don't need them).
Remove the word 'Product' from
select new Product{}
'select new' itself will create an anonymous type whereas you are specifying it as a Product.
From this input: {'hearing' => 1} I need to generate this query
Score.joins(:target_disability).where{ (target_disabilities.name == 'hearing') & (round(total_score) >= 1) }
From this input is {'hearing' => 1, 'mobility' => 2}, I need to generate this:
Score.joins(:target_disability).where{ (target_disabilities.name == 'hearing') & (round(total_score) >= 1) | (target_disabilities.name == 'mobility') & (round(total_score) >= 2) }
And so on...
How can this be generalized? Because my input sometimes has 3 or 4 keys... sometime 1...
Assuming your hash is in my_params:
#scores = Score.joins(:target_disability).where do
my_params.map{|k,v| (target_disabilities.name==k) & (round(total_score)>=v) }.inject(:|)
end