0

I am new to Lambda expression. I want a result from combination of two tables with where clause in lambda Expression , query runs fine but how to get result in variable after processing query??

var Rental = db.AUCDATA_COUPONS.Join(db.AUCDATA_TENORS,
                     c => c.AUCDT_ID,
                     o => o.AUCDT_ID,
                     (c, o) => new { c, o })
               .Where(x => x.o.PRODUCT_ID == "SUKUK" && x.o.ISSUE_DATE == Convert.ToDateTime("02-MAR-12") && x.o.TENOR_ID == "03Y"
                   && x.c.AUCDT_ID == x.o.AUCDT_ID && x.c.COUPON_NXTDT == Convert.ToDateTime("21-NOV-15"))
              .Select(x => x.c.RENTAL_RATE);
H H
  • 263,252
  • 30
  • 330
  • 514
Umair Ikhtiar
  • 17
  • 1
  • 5

2 Answers2

2

db.AUCDATA_COUPONS is an IQueryable<T> (where T is the type of the class representing the table). The extension methods you use (like Join, Where and Select) take this IQueryable<T> and return a new IQueryable<T>.

The last Select returns an IQueryable<int> (or double depending of the type of RENTAL_RATE). The actual query (the lambdas) are only executed when you iterate through that IQueryable. You can do that with foreach

foreach(var rentalRate in Rental)

Maybe a better way is to convert the result to a list or array. This way you would execute the query only once and not again and again with every foreach you execute:

var list = Rental.ToList(); // results in an List<int>
// or
var array = Rental.ToArray(); // results in an int[]

Note that you'll probably need to change your datetime comparisons to

x.o.ISSUE_DATE.Date == new DateTime(2012,3,2)

and

x.c.COUPON_NXTDT.Date == new DateTime(2015,11,21)

for the query to work correctly.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Thanks for reply, but Problem not solve, after using .tolist() there is an errors occurs, – Umair Ikhtiar Aug 10 '16 at 08:09
  • select c.rental_rate from aucdata_coupons c ,aucdata_tenors t where t.product_id='SUKUK' and trunc(t.issue_date) = '02-MAR-12' and t.tenor_id = '03Y' and t.aucdt_id = c.aucdt_id and c.coupon_nxtdt = '21-NOV-15' , please convert it into lambda expression .???? – Umair Ikhtiar Aug 10 '16 at 08:10
  • @UmairIkhtiar Your query actually seems correct. You did not tell us any problem. Please read [how to ask](http://stackoverflow.com/help/how-to-ask). _What_ error occurs? You'll probably need to change your datetime comparisons to `x.o.ISSUE_DATE.Date == new DateTime(2012,3,2)` and `x.c.COUPON_NXTDT.Date == new DateTime(2015,11,21)` – René Vogt Aug 10 '16 at 08:15
  • Thanks , yes this was the problem. query run fine and show result, – Umair Ikhtiar Aug 10 '16 at 08:20
1

You already have the results in the variable. But, depending on what you want to do with them, you can add .ToArray() or .ToList() after .Select(...).

Alan Jurczak
  • 479
  • 5
  • 14
  • Why? You don't need to convert the results to an array to iterate over them. The result of the query is essentially the same as any other IEnumerable (apart from the transparent delayed execution) – Panagiotis Kanavos Aug 10 '16 at 07:54
  • @PanagiotisKanavos - The OP asked to get the results into a variable - it's a vague request, but I think this answer is valid. – Enigmativity Aug 10 '16 at 07:55
  • @Enigmativity the OP already has the results in a variable. There is no need to convert to an array to use them. `ToArray` *converts* the result, just like `ToList` or `ToList` would do. `AsEnumerable` would be enough to eagerly load the results. None of these is needed though to access the data – Panagiotis Kanavos Aug 10 '16 at 07:58
  • @PanagiotisKanavos - I agree with that. It may be that the OP just wants to ensure the query is executed at that particular point in the code. Like I said he's vague about what he wants. – Enigmativity Aug 10 '16 at 08:24
  • @PanagiotisKanavos I agree too. Maybe he just wants to see the results by inspecting the variable, IDK. – Alan Jurczak Aug 10 '16 at 08:45