繁体   English   中英

如何仅选择主表的选定列和明细表中的所有列

[英]How to select only selected column of master table and all column of detail table in Linq

public partial class Product
{
    public Product()
    {
        this.ProductPrimaryPrices = new HashSet<ProductPrimaryPrice>();
        this.ProductSecondaryPrices = new HashSet<ProductSecondaryPrice>();            
    }

    public int ProductId { get; set; }       
    public string Code { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public byte[] Image { get; set; }        

    public virtual ICollection<ProductPrimaryPrice> ProductPrimaryPrices { get; set; }        
    public virtual ICollection<ProductSecondaryPrice> ProductSecondaryPrices { get; set; }
}

从上面的代码中,我只想选择Product实体的ProductIdCodeNameDescription以及ProductPrimaryPricesProductSecondaryPrice所有相关属性,因为从数据库中加载Image byte[]数组需要花费大量时间。 我只需要Product实体中的选定列,以及ProductPrimaryPriceProductSecondaryPrices所有列。

我已经尝试过使用类似如下的匿名类型,但是在如何将匿名类型再次转换回Product遇到了麻烦。

var tempProduct = (from p in ctx.Products.Include("ProductPrimaryPrices").Include("ProductSecondaryPrices")
                   select new 
                          {
                              ProductId = p.ProductId,                                   
                              Code = p.Code,
                              Name = p.Name,
                              Description = p.Description,                                    
                              ProductPrimaryPrices = p.ProductPrimaryPrices,
                              ProductSecondaryPrices = p.ProductSecondaryPrices                                    
                          }
                  ).ToList();

谁能帮我吗?

最后,经过一番头脑风暴,我得到了答案

现在,我可以通过使用反射将匿名类型转换为Product类型。 我已经实现了一种将匿名类型转换为“产品”类型的方法,然后一个接一个地循环我通过调用新实现的方法将匿名类型转换为Product类型。

以下是一个实现

public List<Product> GetProduct()
{
    List<Product> products = new List<Product>();
    using (var ctx = new PosEntities())
    {
        var tempProducts = 
            (from p in ctx.Products.Include("ProductPrimaryPrices").Include("ProductSecondaryPrices").Include("ProductsModifiers")
            where (p.MenuGroupId == menuGroupId && p.Active) && (p.ProductPrimaryPrices.Any(x => x.OutletId == outletId && x.Active))
            select new 
            {
                ProductId = p.ProductId,
                Code = p.Code,
                Name = p.Name,
                Description = p.Description,                
                ProductPrimaryPrices = p.ProductPrimaryPrices,
                ProductSecondaryPrices = p.ProductSecondaryPrices,
                ProductsModifiers = p.ProductsModifiers
            }).ToList();                    

        foreach (object anonymous in tempProducts)
        {
            Product product = (Product)Utility.ToType<Product>(anonymous, new Product());
            products.Add(product);
        }
        return products;
    }
}

以下是将匿名类型转换为Product类型的方法

public static class Utility
    {
        public static object ToType<T>(this object obj, T type)
        {
            //create instance of T type object:            
            object tmp = Activator.CreateInstance(Type.GetType(type.ToString()));

            //loop through the properties of the object you want to covert:          
            foreach (PropertyInfo pi in obj.GetType().GetProperties())
            {
                try
                {
                    //get the value of property and try to assign it to the property of T type object:
                    tmp.GetType().GetProperty(pi.Name).SetValue(tmp, pi.GetValue(obj, null), null);
                }
                catch (Exception ex)
                {
                    // Logging.Log.Error(ex);
                }
            }
            //return the T type object:         
            return tmp;
        }       
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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