I have a list of objects and I would like to compare them by ids field. However, I would like to item with id=3 to show first, and then id=0, id=1 and so on.
list = list.sortedWith(compareBy<MyItem> {it.id})
I was trying a lot of combinations but don't know where to add if statement.
Greetings
list = list.sortedWith(Comparator { a, b -> when {
a.id == 3 -> -1
b.id == 3 -> 1
else -> Integer.compare(a.id, b.id)
}})
Related
I have created an activeadmin filter that has the following choices on filtering the table data in its drop-down menu.
Choice A
Choice B
Choice C
Choice D
I want to add a fifth choice F that would either choice B or C (that is the results of both B and C).
Choice A
Choice B
Choice C
Choice D
Choice F = either B or C
The choices are stored in a table called Coupons in its title field (i created a row having the choice F). So far, I tried to employ a ransack to do the trick, without being quite sure.
//app/admin/user.rb
filter :has_right_choice, label: 'Filter by the right choice', collection: Coupon.all, as: :select
//app/models/user.rb
ransacker :has_right_choice, :formatter => ->(title) {
if (title == 'Choice F')
Coupon.all.where("title = 'Choice B' OR title = 'Choice C'")
else
Coupon.all
end
} do |parent|
parent.table[:id]
end
But my solution doesn't work. Is there a better approach instead of ransack ? If not, any suggestion ?
==========================================================================
EDIT: solution
Choice A # lets say that this choice has id = 1 in Coupons table
Choice B # id = 2
Choice C # id = 3
Choice D # id = 4
Choice F # id = 5
Also lets say that Users table has a field choice_id that refers to the Choices table.
We modify the faulty code as follows:
//app/admin/user.rb
filter :choice_id_in, label: 'Filter by the right choice', collection: Coupon.all, as: :select
//app/models/user.rb
ransacker :choice_id,
:formatter => -> (coupon_id) {
if (coupon_id == "5")
ids = User.all.where("choice_id = 3 OR choice_id = 4").map(&:id)
else
ids = User.where("choice_id = ?", coupon_id).map(&:id)
end
(ids.empty?) ? ids << 0: ids #activeadmin translates the queries into IN operator, may get syntax error if empty
# id = 0 is non-existent in Users as id >= 1
ids #maybe is not needed
} do |parent|
parent.table[:id]
end
Suggestions for improvement are welcome. Thanks #berker for the guidance.
Given a function f:ValueA -> ValueB, how could I map an IGrouping of type IGrouping<Key, ValueA> to IGrouping<Key, ValueB>?
Problem instance:
Say you have this type:
TaggedItem = { Tag:Tag ; Item:Item }
and this query:
query {
for i in taggedItems
groupBy i.Tag into g
select g
}
This would give you a seq of type: IGrouping<Tag, TaggedItem>, but I really want a seq of type: IGrouping<Tag, Item>.
The mapping function is: fun taggedItem -> taggedItem.Item
Solution
The solution is to avoid the mapping of groupings and instead do the transformation while doing the group, using groupValBy, as pointed by the selected answer. The selected answer also shows how to do the mapping from one type of grouping to the other, if you insist.
query {
for i in taggedItems
groupValBy i.Tag i.Item into g
select g
}
How about this?
let mapGrouping f (xs : IGrouping<_,_>) =
let projection = xs |> Seq.map (fun x -> xs.Key, f x)
(projection.GroupBy (fst, snd)).First()
From your code example, I think you want this:
query {
for i in taggedItems do
groupValBy i.Item i.Tag into g
select g
}
At https://msdn.microsoft.com/en-us/library/hh225374.aspx, we learn that groupValBy "selects a value for each element selected so far and groups the elements by the given key."
I am struggling once again so any help would be gratefully received.
I have the following LINQ that pulls back a list of data:
public static List<tblWeight> GetWeights(string memberid, string locationid, string buyer, string subcategory, string product)
{
MyEntity getweights = new MyEntity ();
var r = (from p in getweights.tblWeights
where p.MemberId == memberid &&
p.LocationId == locationid
select p);
if (buyer != "Not Specified")
r = r.Where(p => p.UnitUserField1 == buyer);
if (subcategory != "Not Specified")
r = r.Where(p => p.UnitUserField2 == subcategory);
if (product != "Not Specified")
r = r.Where(p => p.IDDesc == product);
return r.ToList();
}
Lovely!
What I would like to do now is based upon this result set and the unit IDs (IDDesc), I then go to tblPurchase, pull back a few columns from tblPurchases and group the columns.
So for example, we have tblWeight looking like so:
MemberID LocationID Buyer SubCategory IDDesc
1 1 Cat1 Sub1 ab
1 1 Cat1 Sub1 abc
1 1 Cat1 Sub2 abcd
The user makes a search for Sub1 in subcategory and the above LINQ does the trick and pulls back the first two rows from above. Fine.
What I need the LINQ to do now is to go to tblPurchases:
MemberID LocationID IDDesc SupplierID SupplierStatus
1 1 ab Sup1 Live
1 1 abc Sup1 Live
1 1 abcd Sup2 Dead
And then pull back the following result so it is joined on MemberID, LocationID and IDDesc but just selects tblPurchases.
Sup1 Live (or all columns in tblPurchases, just grouped/distinct)
I have tried to add in a join and on but no matter how many different variations, I still come across the red squiggle of doom!!!
If anyone can help, beer/kiss is on offer again.
The following LINQ query should do what you want:
var result = from w in tblWeight
where w.SubCategory == "Sub1"
join p in tblPurchases on
new { w.MemberID, w.LocationID, w.IDDesc } equals
new { p.MemberID, p.LocationID, p.IDDesc }
group p by new { p.SupplierID, p.SupplierStatus } into pg
select pg.Key;
The variable result is a list containing tuples of SupplierID and SupplierStatus.
If you also want to put the conditional parts in there, it gets a little more complicated. Here's how to do it:
var weights = from w in tblWeight
select w;
weights = weights.Where(w => w.SubCategory == "Sub1");
// You can add additional where clauses here.
// Now join with tblPurchases and group by SupplierID and SupplierStatus.
var result =
weights.Join(tblPurchases,
w => new { w.MemberID, w.LocationID, w.IDDesc },
p => new { p.MemberID, p.LocationID, p.IDDesc },
(w, p) => p)
.GroupBy(p => new { p.SupplierID, p.SupplierStatus },
(k, ps) => new
{
k.SupplierID,
k.SupplierStatus,
TotalQty = ps.Sum(p => p.PurchaseQty)
});
I've been looking through related LINQ questions here trying to figure this one out, but I'm having some trouble converting a SQL query of mine to the equivalent LINQ to Entities version.
select companies.CommpanyName,
job.Position,
count(offers.jobID) As Offered,
job.Openings,
job.Filled
from jobs
left outer join offers on jobs.ID = offers.JobID
join membership.dbo.individuals on jobs.UserID = individuals.ID
join membership.dbo.companies on individuals.CompanyID = companies.ID
where jobs.Hidden = 0
group by offers.JobID,
companies.CommpanyName,
job.Position,
job.Openings,
job.Filled
I've done left outer joins in LINQ before similar to this example but I'm not sure how to combine the count and group statements with this to get the desired result:
CompanyName Position Offered Openings Filled
1 Exmaple Co. Job X 0 2 0
2 Example Co. Job Y 4 6 3
3 Test Co. Job Z 1 1 1
The query is further complicated by the fact that it needs to utilize two separate data contexts. I apologize for the lack of example code, but I'm really not sure how to start this, my LINQ-fu is still weak.
Update:
This is the solution I arrived at with Craig's help, had to use LINQ to Objects because of the unfortunate multiple context setup, JobWithOfferCounts is not an entity object:
IEnumerable<Job> t = context1.JobSet.Include("Offers").Include("Contacts").Where(j => j.Hidden == false);
IEnumerable <JobWithOfferCounts> r = (from j in t
join i in context2.IndividualSet on j.UserID equals i.ID
join c in context2.CompanySet on i.CompanyID equals c.ID
select new JobWithOfferCounts()
{
JobObject = j,
CompanyID = Convert.ToInt32(c.ID),
CompanyName = c.HostName,
OfferCount = j.offers.Count(o => o.Rejected == false),
FilledCount = j.offers.Count(o => o.Accepted == true),
PendingCount = j.offers.Count(o => o.Accepted == false && o.Rejected == false)
});
return r;
I can't see why you have individuals in your query, or why you group by offers.JobID when it (unlike jobs.JobId) could be null, but here's a first stab:
var q = from c in Context.Companies
from i in c.Individuals
from j in i.Jobs
where j.Hidden == 0
select new
{
CompanyName = c.CompanyName,
Position = j.Position,
Offered = j.Offers.Count(),
Openings = j.Openings,
Filled = j.Filled
};
It's rarely correct to use join in LINQ to Entities or LINQ to SQL.
I have the following expression in linq (its a join) and i am selecting into "J" because i need to use J later (currently i just selecting J but once i have this fixed i plan on use J within another subquery after)
But it won't let me supply a where using the "V" side hence v.IdOFfice is invalid.
I have tried swapping around the joins and that what happens i can't use the "GVT"..
WIth specifying the where it works perfect but i need to specify 2 wheres that are present in the 2 tables ... hence IdOffice and IdTariff are in there own tables .. they are not both ....
(from gvt in Tariffs
join v in Items
on gvt.IdGroupItem equals v.IdGroupItem
into j
where v.IdOffice == 1 && gvt.IdTariff == 111
select j).Take(50)
Probably something silly, it appears the table specified after the join i am not able to use in the where?
Any ideas?
Thanks
This is basically what i am trying to achieve
from gvt in Tariffs
join v in Items
on gvt.IdGroupItem equals v.IdGroupItem
into j
where v.IdOffice == 1 && gvt.IdTariff == 111
select new
{
id = v.IdItem
Tariff = from j
{
test = j.TariffDesc,
test1 = j.TariffPrice
}
basicaly i end up with 1 record with Id and a field which as many tariffs inside - if this makes sense?
}
Query working great,
it would be nice to be able to use an extension method (c#) like so ... is this possible so i can dynamically set tariff ... so for example i do the query and i have an extension method (which i already use on simple queries) like so
public static IQueryable<Models.ItemTariffCol> WithTariffId(this IQueryable<Models.ItemTariffCol> qry, int tariffId)
{
return from t in qry
where t.IdTarifa == tariffId
select t;
}
this makes it very extensible ? If its a normal where i can do this but the query isn't in the where
Thank you.
You're doing a group join here, since you're using into. This means that for every gvt, you have not one Item, but possibly several (or none). The list of all items is stored in j, as an IEnumerable<Item>. If you want to select all tariffs for which there's at least one item with IdOffice == 1, then you can do it like this:
from gvt in Tariffs
join v in Items
on gvt.IdGroupItem equals v.IdGroupItem
into j
where gvt.IdTariff == 111 && j.Any(v => v.IdOffice == 1)
...
After the answer edit, it seems that you've started from the wrong direction as well - so far as I can see, you want a list of tariffs for every item, not the list of items for every tariff. For that, you need to reverse your join:
from item in Items
join tariff in Tariffs
on item.IdGroupItem equals tariff.IdGroupItem
into tariffs
where item.IdOffice == 1
select new
{
Id = item.IdItem,
Tariffs = from tariff in tariffs
where tariff.IdTariff == 111
select new { tariff.TariffDesc, tariff.TariffPrice }
}
Or you could filter tariffs right in the join:
from item in Items
join tariff in (from t in Tariffs where t.IdTariff == 111 select t)
on item.IdGroupItem equals tariff.IdGroupItem
into tariffs
where item.IdOffice == 1
select new
{
Id = item.IdItem,
Tariffs = from tariff in tariffs
select new { tariff.TariffDesc, tariff.TariffPrice }
}