how to write this linq query in expression tree syntax
from x in 100.To(999)
from y in 100.To(999)
let product = x * y
where product.IsEven()
select product
The equivalent to the 'from x from y select' is the 'SelectMany' keyword used with an additional 'Select':
100.To(999).SelectMany(x => 100.To(999).Select(y => x * y))
.Where(x => x.IsEven())
Related
Would this Spring Data JPA method:
List<Foo> findAllByXOrYAndZ();
represent which logical case of the two:
(X || Y) && Z
X || (Y && Z)
And how can I represent the other one with Spring Data generated queries?
It represents: X || (Y && Z).
The Query that Hibernate will execute, in the case of List<Foo> findAllByXOrYAndZ();, would be:
select
*columns*
from
*table*
where
x = ?
or y = ?
and z = ?
Pay attention to the WHERE clause of the query. Knowing that AND has higher precedence over OR operation, the equivalent WHERE clause would be:
where
x = ? or ( y = ? and z = ? )
Hibernate automatically removes the parentheses because they are not needed.
I generally would avoid using such methods, because they are not very readable. You should use a Custom Query with #Query instead.
Hi I am trying to work on finding the count which is higher than average using the below hive statement
Select x, Count(x) as y from data
group by x
Having Count(x) >= (select Avg(z.count1) as aveg
from (select x, Count(x) as count1 from data group by x ) z) ;
I am receiving error as ParseException line 1:87 cannot recognize input near 'Select' 'Avg' '(' in expression specification
select x
,cnt_x
from (select x
,count(x) as cnt_x
,avg (count(x)) over () as avg_cnt_x
from data
group by x
) t
where cnt_x >= avg_cnt_x
I've been searching but I can't find the right answer for my question.
The problem is:
a predicate
initial_estates([rb(1,1),rb(2,2),rb(3,5)]).
I want to use findall to get me a list of all the second term of rb's.
Like in this example [1,2,5]
Anyone can help
You can use lists:member/2 to interactively find all values of the pairs rb/2 in your list:
?- initial_estates(L),member(rb(X,Y),L).
L = [rb(1,1),rb(2,2),rb(3,5)],
X = Y = 1 ? ;
L = [rb(1,1),rb(2,2),rb(3,5)],
X = Y = 2 ? ;
L = [rb(1,1),rb(2,2),rb(3,5)],
X = 3,
Y = 5 ? ;
no
And based on that findall to get all second elements (Y) in one list (Res):
?- findall(Y,(initial_estates(L),member(rb(X,Y),L)),Res).
Res = [1,2,5]
Alternatively, you could also write a predicate, say rb_2nd/2, that describes the second argument to be the second element in the pair that is the first argument:
rb_2nd(rb(X,Y),Y).
And then use apply:maplist/3 to apply that relation to the list described by initial_estates/1:
?- initial_estates(L), maplist(rb_2nd,L,Res).
L = [rb(1,1),rb(2,2),rb(3,5)],
Res = [1,2,5]
I find the latter easier to read than the query using findall/3.
Here come a little ABAP challenge:
For an ABAP projet, i must build from an internal table with 2 columns (example1) another table containing all combinations possibles (example2).
"X" columns represent the parameter. "Y" represent the parameter value.
example1:
X(param)
Y(value)
A a1
A a2
A a3
B b1
B b2
C c1
C c2
In the result table (example2):
We must get all combinations with a numeric id (on 3 columns).
The new "z" column represent the combination id. For each combination there is a number of lines equal to the number of dictinct parameters(in our case 3 line for A,B and C).
"x" column still represent the parameter and "y" column the associated value.
example2:
z(combi num)
x(param)
y(value)
1 A a1
1 B b1
1 C c1
2 A a1
2 B b1
2 C c2
3 A a1
3 B b2
3 C c1
4 A a1
4 B b2
4 C c2
etc...
etc...
etc...
12 A a3
12 B b2
12 C c2
Another remark is that the number of parameters and the number of values per parameters is not fixed (the initial internal table can evolve a lot and so the combinations possibles).
We maybe need recursion but i'm not sure of it.
Here is a non-recursive way to do it, you might have to rewrite the parts that use the new 740 syntax. The idea is pretty simple, first transform the data into an internal table with one entry per parameter containing a table with the possible values, the LOOP loop. From there it is a simple matter of going through all the combinations and adding these to another internal table, the WHILE loop.
REPORT z_algorithm.
TYPES: ty_param TYPE char1,
ty_value TYPE char2,
BEGIN OF ty_struct,
x TYPE ty_param,
y TYPE ty_value,
END OF ty_struct,
BEGIN OF ty_combi,
z TYPE i,
s TYPE ty_struct,
END OF ty_combi.
TYPES: BEGIN OF ty_param_struct,
x TYPE ty_param,
ys TYPE STANDARD TABLE OF ty_value WITH DEFAULT KEY,
ix TYPE i,
END OF ty_param_struct.
DATA: tab TYPE STANDARD TABLE OF ty_struct,
params TYPE STANDARD TABLE OF ty_param_struct,
done TYPE abap_bool VALUE abap_false,
z TYPE i VALUE 0,
overflow TYPE abap_bool VALUE abap_false,
combis TYPE STANDARD TABLE OF ty_combi.
START-OF-SELECTION.
APPEND VALUE: #( x = 'A' y = 'a1' ) TO tab,
#( x = 'A' y = 'a2' ) TO tab,
#( x = 'A' y = 'a3' ) TO tab,
#( x = 'B' y = 'b1' ) TO tab,
#( x = 'B' y = 'b2' ) TO tab,
#( x = 'C' y = 'c1' ) TO tab,
#( x = 'C' y = 'c2' ) TO tab.
LOOP AT tab ASSIGNING FIELD-SYMBOL(<tab>).
READ TABLE params WITH KEY x = <tab>-x ASSIGNING FIELD-SYMBOL(<param>).
IF sy-subrc NE 0.
APPEND INITIAL LINE TO params ASSIGNING <param>.
<param>-x = <tab>-x.
<param>-ix = 1.
ENDIF.
APPEND <tab>-y TO <param>-ys.
ENDLOOP.
WHILE done EQ abap_false.
ADD 1 TO z.
overflow = abap_true.
done = abap_true.
LOOP AT params ASSIGNING <param>.
READ TABLE <param>-ys INDEX <param>-ix ASSIGNING FIELD-SYMBOL(<y>).
APPEND VALUE #( z = z s-x = <param>-x s-y = <y> ) TO combis.
IF overflow EQ abap_true.
ADD 1 TO <param>-ix.
ENDIF.
IF <param>-ix GT lines( <param>-ys ).
overflow = abap_true.
<param>-ix = 1.
ELSE.
overflow = abap_false.
done = abap_false.
ENDIF.
ENDLOOP.
ENDWHILE.
I thought I had got this to work before, but I just don't see it:
discounts is a Dictionary<PartType, double>. data is a List<PartType>.
var d = discounts.Keys.SelectMany(
k =>
data.Where( l => l.PartTypeID.Equals( k.PartTypeID ) ) )
.Select( s => new { k, l } );
The error is, the name 'k' (and 'l') does not exist in the current context.
What I eventually want to do is apply the double from the dictionary to all the matching PartTypes in data.
I suspect you mean:
var d = discounts.Keys.SelectMany(
k => data.Where(l => l.PartTypeID.Equals(k.PartTypeID)),
(k, l) => new { k, l });
... but it's hard to tell without more information. To be honest it looks like you really want a join, e.g.
var d = discounts.Keys.Join(data,
k => k.PartTypeID, // Key from discounts.Keys
l => l.PartTypeID, // Key from data
(k, l) => new { k, l }); // Projection from the two values
The l is in the context of the lambda in the Where. I think you mean s => new {k, s}. Also your parentheses seem wrong. That last parentheses on line 3 should be on line 4.