views:

127

answers:

1

Hi there,

I written a small query and in Linqpad its working well but (see below) Tariffs is not returned as Iqueryable, does anyone know how to fix this ?

Basically see Tariffs = new ....,

from v in House
join gvt in
    (from t in MyTariffs where t.Id == 3 select t)
on v.IdTariff equals gvt.Id
select new
{
   Id = v.Id,
   Tariffs = new
   { 
     Deposit = gvt.CurrentDeposit
   }
}

I did try this but its invalid because gvt isn't a table or something?

from v in House
join gvt in
    (from t in MyTariffs where t.Id == 3 select t)
on v.IdTariff equals gvt.Id
select new
{
   Id = v.Id,
   Tariffs = from x in gvt   // NOTICE i am doing from x in gvt... But it fails..
   select new
   { 
     Deposit = gvt.CurrentDeposit
   }
}

Of course gvt contains just the values i want because it has the inner join...

I could do just pull directly from my MyTariffs (which works it returns Iqueryable) but then i have too much info as its not taking into consideration the join which i did in gvt?

from v in House
join gvt in
    (from t in MyTariffs where t.Id == 3 select t)
on v.IdTariff equals gvt.Id
select new
{
   Id = v.Id,
   Tariffs = from x in MyTariffs // THIS has nothing to do with my join
   select new
   { 
     Deposit = gvt.CurrentDeposit
   }
}
+2  A: 

Select the data in a subquery -- are you sure that Id == 3 and Id == v.IdTariff? If that's really the case, then you could add a where clause to the outer query to select only v when v.IdTariff == 3. I'm assuming, though, that you want them all.

var q = from v in House
        select new {
            Id = v.Id,
            Tariffs = (from g in MyTariffs
                       where g.Id == v.IdTariff
                       select g.CurrentDeposit)
        };

Grouped example (uncompiled/untested), in response to your comments.

var q = from v in House
        join g in (from t in MyTariffs where t.Id == 3 select t)
        group by v.Id into gvt
        select new {
            Id = gvt.Key,
            Tariffs = gvt.g
        };
tvanfosson
Thanks tvanfosson! The problem being i have some more fairly complex joins furthr down... Is it not possible to do what i was doing ??
mark smith
Hi .. i just did some tests also ... and it seems putting the clause in the where like so takes longer to process... I suppose this is logical because i am select from MyTariffs which is a full table without any joins.. My query takes 7 seconds with my original query that doesn't return Iqueryable and 13 seconds with the where....
mark smith
If you want to have a queryable collection from the second table in the join, you may need to do a group by on the Id column, then query against the grouped elements.
tvanfosson
Hi thanks fro your comments, i accepted the answer as i have it working .... thanks once again..
mark smith