English 中文(简体)
ef cf linq populatione
原标题:ef cf linq populate ignored property

我确信,必须事先提出这一要求,但我找不到一个很好的办法去寻找。

我有以下一类:

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

这里是用来 gr磨供应商的询问。

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

            return query.ToArray();
        }
    }

• 如何将产品行业分类,使之与类似

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

在主问中,我能补充一下吗?

Thanks, Andrew

最佳回答

我将尝试这样做:

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。 页: 1

问题回答

暂无回答




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签