Help with linq query with subqueries
can anyone help?, i am stuck with a linq query..
basically i have a standard linq query that returns fields, 1 field (insurances) is actually another linq query like so
// original from this in etc not included to keep msg short>
select new Models.Custom.House.Insurance()
{
Id = v.IdHouse,
Insurances = from gt in GroupHouseTariffs
join i in InsuranceGroup
on new { gt.IdTariff, gt.IdGroup}
equals
new { i.IdTariff, i.IdGroup}
select new
{
InsuranceId = i.Id,
Price = i.Price
}
basica开发者_开发百科lly insurance is injected into the insurance property from Models.Custom.House, it works as i can see it in my debug ... i have 4 records in the insurance .. insurance is defined like this in House which is basically an Iqueryable of another small class..
public IQueryable<Insurance> Insurances { get; set;}
So i tried to write an extension method, like so
public static IQueryable<House> WithInsuranceId(this IQueryable<House> qry, int insuranceId)
{
return from h in qry
where h.Insurance .. // BUT here is the problem i don't see my properties, I see plenty of linq methods
}
I should be able to see insuranceId and do this , no?
return from h in qry
where h.Insurance.InsuranceId == 1;
Here is the class (its very small)
public class Insurance
{
public int? InsuranceId { get; set; }
public float? price{ get; set; }
}
Maybe there is some sort of special lambda i need to know about :-) ?
Any help really appreciated, thank you.
Are there typos in the sample you posted?
I noticed the following:
// No object name specified, Price has a capital P
select new
{
InsuranceId = i.Id,
Price = i.Price
}
// Price has a small p
public class Insurance
{
public int? InsuranceId { get; set; }
public float? price{ get; set; }
}
Now, your actual query looks wrong to me as well.
House.Insurance has a property called ID and a property called Insurances, which is a collection. Yet your query states:
return from h in qry
where h.Insurance.InsuranceId == 1;
Do you mean:
return from h in qry
where h.Insurance.Id == 1 select h;
Or:
return from h in qry
where h.Insurance.Insurances.Contains(i => i.InsuranceID ==1) select h;
精彩评论