I have a issue with OrderByDescending in Orchard CMS
Example Data:
ID Name DomainId
1 First 2
2 Join 3
3 Peter 1
4 Abert 1
5 saha 2
with LinQ to SQL code here:
IQueryable().OrderByDescending(r=> r.DomainId == 2)
it returns the correct result !
ID Name DomainId
1 First 2
5 saha 2
2 Join 3
3 Peter 1
4 Abert 1
but with OrchardCMS
IContentQuery().OrderByDescending(r=> r.DomainId == 2)
it returns the incorrect result and it will order from large to small
ID Name DomainId
2 Join 3
1 First 2
5 saha 2
3 Peter 1
4 Abert 1
Why incorrect ? and how to fix
Please help me!
i want to return results with sort by DomainId
Example: with DomainId = 2 => IContentQuery().OrderByDescending(r=> r.DomainId == 2)
ID Name DomainId
1 First 2
5 saha 2
2 Join 3
3 Peter 1
4 Abert 1
Example: with DomainId = 3 => IContentQuery().OrderByDescending(r=> r.DomainId == 3)
ID Name DomainId
2 Join 3
1 First 2
5 saha 2
3 Peter 1
4 Abert 1
Try:
.OrderByDescending(r=> r.DomainId == 2 ? 1 : 0)
IContentQuery().OrderByDescending(r=> r.DomainId) is the right approach I think. The expression (r=> r.DomainId == 2) is it evaluate right result?
Related
i want Cumulative count of zero only in column c grouped by column a and sorted by b if other number the count reset to 1
this a sample
df = pd.DataFrame({'a':[1,1,1,1,2,2,2,2],
'b':[1,2,3,4,1,2,3,4],
'c':[10,0,0,5,1,0,1,0]}
)
i try next code that work but if zero appear more than one time shift function didn't depend on new value and need to run more than one time depend on count of zero series
df.loc[df.c == 0 ,'n'] = df.n.shift(1)+1
i try next code it done with small data frame but when try with large data take a long time and didn't finsh
for ind in df.index:
if df.loc[ind,'c'] == 0 :
df.loc[ind,'new'] = df.loc[ind-1,'new']+1
else :
df.loc[ind,'new'] = 1
pd.DataFrame({'a':[1,1,1,1,2,2,2,2],
'b':[1,2,3,4,1,2,3,4],
'c':[10,0,0,5,1,0,1,0]}
The desired result
a b c n
0 1 1 10 1
1 1 2 0 2
2 1 3 0 3
3 1 4 5 1
4 2 1 1 1
5 2 2 0 2
6 2 3 1 1
7 2 4 0 2
Try use cumsum to create a group variable and then use groupby.cumcount to create the new column:
df.sort_values(['a', 'b'], inplace=True)
df['n'] = df['c'].groupby([df.a, df['c'].ne(0).cumsum()]).cumcount() + 1
df
a b c n
0 1 1 10 1
1 1 2 0 2
2 1 3 0 3
3 1 4 5 1
4 2 1 1 1
5 2 2 0 2
6 2 3 1 1
7 2 4 0 2
I have a dataframe looks like,
A B
1 2
1 3
1 4
2 5
2 6
3 7
3 8
If I df.groupby('A'), how do I turn each group into sub-dataframes, so it will look like, for A=1
A B
1 2
1 3
1 4
for A=2,
A B
2 5
2 6
for A=3,
A B
3 7
3 8
By using get_group
g=df.groupby('A')
g.get_group(1)
Out[367]:
A B
0 1 2
1 1 3
2 1 4
You are close, need convert groupby object to dictionary of DataFrames:
dfs = dict(tuple(df.groupby('A')))
print (dfs[1])
A B
0 1 2
1 1 3
2 1 4
print (dfs[2])
A B
3 2 5
4 2 6
Id
1
2
3
4
2
3
3
3
Questions
create one new column and that is newid
output should be like this.
id newid
1 1
2 1
3 1
4 1
2 0
3 0
3 0
3 0
Please suggest me how can I do it and which formula to be used in excel
I'm trying to implement A006751 in J. It's pretty easy to do in Haskell, something like:
concat . map (\g -> concat [show $ length g, [g !! 0]]) . group . show
(Obviously that's not complete, but it's the basic heart of it. I spent about 10 seconds on that, so treat it accordingly.) I can implement any of this fairly easily in J, but the part that eludes me is a good, idiomatic J algorithm that corresponds to Haskell's group function. I can write a clumsy one, but it doesn't feel like good J.
Can anyone implement Haskell's group in good J?
Groups are usually done with the /. adverb.
1 1 2 1 </. 'abcd'
┌───┬─┐
│abd│c│
└───┴─┘
As you can see, it's not sequential. Just make your key sequential like so (essentially determining if an item is different from the next, and do a running sum of the resulting 0's and 1's):
neq =. 13 : '0, (}. y) ~: (}: y)'
seqkey =. 13 : '+/\neq y'
(seqkey 1 1 2 1) </. 'abcd'
┌──┬─┬─┐
│ab│c│d│
└──┴─┴─┘
What I need then is a function which counts the items (#), and tells me what they are ({. to just pick the first). I got some inspiration from nubcount:
diffseqcount =. 13 : ',(seqkey y) (#,{.)/. y'
diffseqcount 2
1 2
diffseqcount 1 2
1 1 1 2
diffseqcount 1 1 1 2
3 1 1 2
If you want the nth result, just use power:
diffseqcount(^:10) 2 NB. 10th result
1 3 2 1 1 3 2 1 3 2 2 1 1 3 3 1 1 2 1 3 2 1 2 3 2 2 2 1 1 2
I agree that /. ( Key ) is the best general method for applying verbs to groups in J. An alternative in this case, where we need to group consecutive numbers that are the same, is dyadic ;. (Cut):
1 1 0 0 1 0 1 <(;.1) 3 1 1 1 2 2 3
┌─┬─────┬───┬─┐
│3│1 1 1│2 2│3│
└─┴─────┴───┴─┘
We can form the frets to use as the left argument as follows:
1 , 2 ~:/\ 3 1 1 1 2 2 3 NB. inserts ~: in the running sets of 2 numbers
1 1 0 0 1 0 1
Putting the two together:
(] <;.1~ 1 , 2 ~:/\ ]) 3 1 1 1 2 2 3
┌─┬─────┬───┬─┐
│3│1 1 1│2 2│3│
└─┴─────┴───┴─┘
Using the same mechanism as suggested previously:
,#(] (# , {.);.1~ 1 , 2 ~:/\ ]) 3 1 1 1 2 2 3
1 3 3 1 2 2 1 3
If you are looking for a nice J implementation of the look-and-say sequence then I'd suggest the one on Rosetta Code:
las=: ,#((# , {.);.1~ 1 , 2 ~:/\ ])&.(10x&#.inv)#]^:(1+i.#[)
5 las 1 NB. left arg is sequence length, right arg is starting number
11 21 1211 111221 312211
I have the following table "GroupPriority":
Id Group Priority
1 1 0
2 2 0
3 3 0
4 2 1
5 1 1
6 2 2
7 3 1
I would like to group these on "Group", order them by "Priority" and then get the one in each "Group" with the highest Priority with the use of linq and subsonic 3.
In this case the result would be:
Id Group Priority
5 1 1
6 2 2
7 3 1
The sql would look like this:
SELECT *
FROM GroupPriority
WHERE (Priority =
(SELECT MAX(Priority)
FROM GroupPriority
WHERE (Group = GroupPriority.Group)))
Thanks
Got the solution:
var group_query = new Query<GroupPriority>(provider);
var items = from gp in group_query
where gp.Priority ==
(from gp_sub in group_query
where gp_sub.Group == gp.Group
select gp_sub.Priority).Max()
select gp;