ef cf linq 填充忽略的属性

发布于 2024-12-05 03:26:00 字数 1020 浏览 0 评论 0原文

我确信以前已经问过这个问题,但我找不到一个好的方法来搜索它。

我有一个像下面这样的类

public class Vendor
{
    public int VendorId { get; set; }
    public string Name { get; set; }
    public int ProductCount { get; set; }
}

我有一个像下面这样的配置类设置

public class VendorConfiguration : EntityTypeConfiguration<Vendor>
{
    public VendorConfiguration()
    {
        Property(p => p.Name).IsRequired().HasMaxLength(128);
        Ignore(v => v.ProductCount);
    }
}

这是我用来获取供应商的查询。

    public Vendor[] GetVendors()
    {
        using (var db = new UbidContext())
        {
            var query = (from vendor in db.Vendors
                                        select vendor);

            return query.ToArray();
        }
    }

我如何使用看起来类似于的子查询填充 ProductCount

ProductCount = (from vend in db.VendorProducts
 where vend.VendorId == id
 select vend).Count()

有没有办法将其添加到主查询中,这样我只对数据库进行 1 次调用?

谢谢, 安德鲁

I'm sure this has to have been asked before, but I couldn't find a good way to search on it.

I have a class like the following

public class Vendor
{
    public int VendorId { get; set; }
    public string Name { get; set; }
    public int ProductCount { get; set; }
}

I have a configuration class setup like

public class VendorConfiguration : EntityTypeConfiguration<Vendor>
{
    public VendorConfiguration()
    {
        Property(p => p.Name).IsRequired().HasMaxLength(128);
        Ignore(v => v.ProductCount);
    }
}

Here is the query i use to grab the vendors.

    public Vendor[] GetVendors()
    {
        using (var db = new UbidContext())
        {
            var query = (from vendor in db.Vendors
                                        select vendor);

            return query.ToArray();
        }
    }

How could I populate ProductCount with a subquery that would look similar to

ProductCount = (from vend in db.VendorProducts
 where vend.VendorId == id
 select vend).Count()

Is there a way I can add that to the main query, so I'm only making 1 call to the db?

Thanks,
Andrew

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

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

发布评论

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

评论(1

缺⑴份安定 2024-12-12 03:26:00

我会这样尝试:

public Vendor[] GetVendors()
{
    using (var db = new UbidContext())
    {
        var query = from vendor in db.Vendors
                    join vp in db.VendorProducts
                        on vendor.VendorId equals vp.VendorId
                    into vendorProducts
                    select new
                    {
                        Vendor = vendor,
                        ProductCount = vendorProducts.Count()
                    };

        foreach (var item in query)
            item.Vendor.ProductCount = item.ProductCount;

        return query.Select(a => a.Vendor).ToArray();
    }
}

问题是您必须投影到非实体类型(在上面的示例中为匿名),然后将投影的 ProductCount 值复制到投影的 Vendor 中> 退货前逐项进行。

I would try it this way:

public Vendor[] GetVendors()
{
    using (var db = new UbidContext())
    {
        var query = from vendor in db.Vendors
                    join vp in db.VendorProducts
                        on vendor.VendorId equals vp.VendorId
                    into vendorProducts
                    select new
                    {
                        Vendor = vendor,
                        ProductCount = vendorProducts.Count()
                    };

        foreach (var item in query)
            item.Vendor.ProductCount = item.ProductCount;

        return query.Select(a => a.Vendor).ToArray();
    }
}

Problem is that you must project into a non-entity type (anonymous in the example above) and then copy the projected ProductCount value into the projected Vendor item by item before you return it.

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