Finding a column in a DataTable row - linq-to-dataset

Client_Payroll_Items.AsEnumerable().Select((row, index) => new {row, index})
.Where(
(x => x.row.Client_KEY == 3) &&
(x => x.row.Description == "401(k) % of Gross")
)
//.Where(x => x.row.Description == "401(k) % of Gross")
.Select(x=> x.row.Payroll_item_calculation_type_KEY)
.DefaultIfEmpty(-1)
.First();
When I am running it I am getting "Operator '&&' cannot be applied to operands of type 'lambda expression' and 'lambda expression'" error.
Client_Payroll_Items
.AsEnumerable()
.Select((row, index) => new {row, index})
.Where(x => x.row.Client_KEY == 3)
.Where(x => x.row.Description == "401(k) % of Gross")
.Select(x=> x.row.Payroll_item_calculation_type_KEY)
.DefaultIfEmpty(-1)
.First()
The latest one runs and I get what I need.
Question - why the error in the first code?

In your first code sample you have specified the lambda x => multiple times, which is incorrect:
.Where((x => x.row.Client_KEY == 3) && (x => x.row.Description == "401(k) % of Gross"))
It should be specified only once and can be used multiple times, as needed, within the predicate:
.Where(x => x.row.Client_KEY == 3 && x.row.Description == "401(k) % of Gross")

Related

Why does expression (true == true == true) produce a syntax error?

Ruby:
true == true == true
syntax error, unexpected tEQ
vs. JavaScript:
true == true == true
// => true
vs. C:
1 == 1 == 1
// => 1
Association direction, which controls the order of operators having their arguments evaluated, is not defined for the == method, same as for ===, !=, =~ and <=> methods as well (all of which have the same precedence and form a separate precedence group exclusively).
Documentation
Thus evaluation order in case of multiple operators from the list mentioned above being chained in a row should be set explicitly via either
parenthesis ():
(true == true) == true # => true
true == (true == true) # => true
or dot operator . (can be omitted for the last equality check in a row):
true .== true == true # => true
TL;DR The syntax implies that all 3 values are equal this is not what it does in javascript or C, so by ruby giving a syntax error the door is open for this to be implemented in the future.
If I understand the question correctly value_a == value_b == value_c should only return true if they are all equal using == as the comparison operater as shown in this method
# version 1
def compare_3_values(a, b, c)
a == b && a == c && b == c
end
there is another possible expected outcome though. to implement this as shown in the previous answer:
#version 2
def compare_3_values(a, b, c)
(a == b) == c
end
The results are worlds apart.
JavaScript always uses version 2 which is pretty useless as the 3rd item is always being compared against true or false (0 or 1 if the 3rd item is an integer) that's why false == false == true returns true.
The good news is that because ruby gives a syntax error it's the only language that can implement this without breaking everyone's code.
for any other language it would break so much code that even if it were implemented in a later major version there would need to be a flag/setting to turn this on or off for years to come, hence it will never be worthwhile.
Some interesting results in Ruby
false .== false == true
=> true
false .== true == false
=> true
true .== false == false
=> true
false .== false == false
=> false
true .== true == false
false
And in javascript
false == false == true
=> true
false == true == false
=> true
true == false == false
=> true
false == false == false
=> false
true == true == false
=> false
Edit tested in C as well, acts similar to JavaScript in that it compares the result of the first two values against the third value
The first answer is excellent, but just in case it's not completely clear (and people asking why), here are few more examples.
In C, the == operator is left-to-right associative and boolean is represented as 1 (true) and 0 (false), so the first 1 == 1 evaluates to 1 (true) and then you are evaluating the result of first expression with the second. You can try:
2 == 2 == 2 // => 0
Which in C, is evaluated as:
(2 == 2) == 2
1 == 2 // => 0
In Javascript, similarly to C, == is left to right associative. Let's try with 0 this time (although the same example from C would work as well):
0 == 0 == 0
false
Again:
0 == 0 == 0
true == 0 // => false
In Ruby == does not have associative properties, ie. it can't be used multiple times in single expression, so that expression can't be evaluated. Why that decision was made is a question for the author of the language. Further, Ruby doesn't define numeric 1 as a boolean, so 1 == true evaluates to false.
The second answer states there are some "weird" cases in Ruby, but they all evaluate as expected:
(1 == 1) == 1
true == 1 # => false
1 == (1 == 1)
1 == true # => false
1 .== 1 == 1
(1 == 1) == 1
true == 1 # => false
false .== false == true
(false == false) == true
true == true # => true
false .== true == false
(false == true) == false
false == false # => true
true .== false == false
(true == false) == false
false == false # => true
false .== false == false
(false == false) == false
true == false # => false
true .== true == false
(true == true) == false
true == false # => false

How can I structure nHibernate QueryOver where (X AND Y) OR (null AND null)

In SQL ease:
where (Id1 = #iX and Id2 = #iY) OR (id1 is null AND Id2 is null)
In (sudo/failed) nHibernate:
where (c => c.Id1 == iX AND c.Id2 == iY) OR (c => c.Id1 == null AND c.Id2 == null)
All helpful suggestions appreciated.
using criteria:
var r = ses.CreateCriteria<myT>()
.Add(Restrictions.Or(
Restrictions.And(Restrictions.Where<myT>((c) => c.Id1 == iX)
, Restrictions.Where<myT>((c) => c.Id2 == iY)
)
, Restrictions.And(Restrictions.Where<myT>((c) => c.Id1 == null)
, Restrictions.Where<myT>((c) => c.Id2 == null)
)
))
.List<myT>();
Nets a reasonable:
WHERE ((this_.Id1 = #p0 and this_.Id2 = #p1) or (this_.Id1 is null and this_.Id2 is null))
where #p0 = #iX and #p1 = #iY

Ruby Ternary Inside Hash Build

Looking for a way to include a ternary conditional inside a hash assignment.
a = 5
h = {}
h[:alpha] => a > 3 ? true : false # edited twice
h[:alpha] => (a > 3 ? true : false) # edited twice
There has to be a way of shortening this up.
Almost always when a beginner is using a literal true or false, that is unnecessary. In this case, you don't need a ternary at all.
a = 5
h = {}
h[:alpha] = a > 3
h[:alpha] # => true
You need to assign the values by using = (assignment operator) and not =>.
Try:
h[:alpha] = a > 3 ? true : false
Example:
2.1.2-perf :001 > a = 5
=> 5
2.1.2-perf :002 > h = {}
=> {}
2.1.2-perf :005 > h[:alpha] = (a > 3 ? true : false)
=> true
2.1.2-perf :006 > h[:alpha]
=> true
Edit (As per your comments):
2.1.2-perf :014 > user = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
2.1.2-perf :016 > user[1] == "solo" ? "#{user[2]} #{user[3]} (s)" : "#{user[4]} (g)"
=> "5 (g)"

LINQ Extension method .SelectMany() written in another form

How to write this LINQ expression in another .SelectMany() form ?
var result =
from a in numbersA
where a < 3
from b in numbersB
where b < 5
select new { a, b };
?
var result = numbersA.Where(x => x < 3).Select.. ?
This is a rough translation of what the compiler would do:
var result = numbersA.Where(a => a < 3)
.SelectMany(a => numbersB, (a, b) => new { a, b })
.Where(z => z.b < 5)
.Select(z => new { z.a, z.b });
Now you can write this more efficiently as:
var result = numbersA.Where(a => a < 3)
.SelectMany(a => numbersB.Where(b => b < 5),
(a, b) => new { a, b });
... but that's not what the compiler would do. It's not clear whether your aim is to see what the compiler does, or to just write a query.
Something like
var result = numbersA.Where(a => a < 3).SelectMany(a =>
numbersB.Where(b => b < 5).Select(b => new { a, b }));
Note that it maybe be more efficient to filter numbersB once only:
var filteredB = numbersB.Where(b => b < 5).ToArray();
var result = numbersA.Where(a => a < 3).SelectMany(a =>
filteredB.Select(b => new { a, b }));

Query to Method Expression in Linq!

How can i right this in method Expression!
var query =
from l in list where l.Key == "M"
select new { Value = l, Length = l.Length };
You want to turn it into a sequence of extension method calls?
var query = list.Where(l => l.Key == "M")
.Select(l => new { Value = l, Length = l.Length });

Resources