简体   繁体   中英

LINQ; forcing a new {} to return Iqueryable?

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
   }
}

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
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM