使用子查询的 linq 查询帮助

发布于 2024-08-08 18:17:37 字数 1740 浏览 3 评论 0原文

任何人都可以帮忙吗?,我被一个 linq 查询困住了。

基本上我有一个返回字段的标准 linq 查询,1 个字段(保险)实际上是另一个 linq 查询,就像这样

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

基本上保险从 Models.Custom 注入到保险属性中。 House,它的工作原理就像我在调试中看到的那样...我在保险中有4条记录..保险在House中是这样定义的,它基本上是另一个小类的Iqueryable..

       public IQueryable<Insurance> Insurances { get; set;}

所以我尝试编写一个扩展方法,就像这样

    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

    }

我应该能够看到 InsuranceId 并执行此操作,不是吗?

    return from h in qry
               where h.Insurance.InsuranceId == 1;

这是这个类(它非常小),

    public class Insurance
    {
        public int? InsuranceId { get; set; }
        public float? price{ get; set; }
    }

也许我需要了解某种特殊的 lambda :-) ?

非常感谢任何帮助,谢谢。

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
                                       }

basically 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2024-08-15 18:17:37

您发布的示例中是否有拼写错误?

我注意到以下内容:

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

现在,您的实际查询对我来说也是错误的。

House.Insurance 有一个名为 ID 的属性和一个名为 Insurances 的属性,它是一个集合。然而您的查询指出:

 return from h in qry
           where h.Insurance.InsuranceId == 1;

您的意思是:

return from h in qry
           where h.Insurance.Id == 1 select h;

还是:

return from h in qry
           where h.Insurance.Insurances.Contains(i => i.InsuranceID ==1) select h;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文