简体   繁体   English

从LINQ获取另一个类以进行SQL查询

[英]Get another class from LINQ to SQL query

I am a little weak in LINQ to SQL so will try to explain my problem. 我在LINQ to SQL方面有些虚弱,因此将尝试解释我的问题。

I have a method as follows (simplified to explain it better): 我有一个方法如下(简化以更好地解释它):

public static List<ic_ProductData> GetCompleteSimilarProductsWithApplyButton(InfoChoiceAdminDataContext db)
{

    var products = (from
                        p in db.ic_ProductDatas
                    join proddef in db.ic_ProductDefs on p.ProductDefId equals proddef.ProductDefId

                    select p
                        ).ToList();

    return products;
}

ic_ProductData and ic_ProductDefs are tables in my database ic_ProductDataic_ProductDefs是我数据库中的表

The ic_ProductData class contains a manually created property as: ic_ProductData类包含一个手动创建的属性,如下所示:

public ic_ProductDef RelatedProductDef { get; set; }

I want to modify the above LINQ to SQL query so that I can populate this property. 我想将上面的LINQ修改为SQL查询,以便可以填充此属性。

Please note I do not want another call to the database. 请注意,我不想再次调用数据库。

Also there are a lot of properties in ic_ProductData so I want to avoid mapping each and every property ic_ProductData也有很多属性,因此我想避免映射每个属性

Something to the effect of the following (obviously the below is wrong): 以下内容的影响(显然以下内容是错误的):

public static List<ic_ProductData> GetCompleteSimilarProductsWithApplyButton(InfoChoiceAdminDataContext db)
{

    var products = (from
                        p in db.ic_ProductDatas
                    join proddef in db.ic_ProductDefs on p.ProductDefId equals proddef.ProductDefId
                    //trying to change here
                    select new ic_ProductData
                    {
                        //do something with p here so that all the properties of new object gets filled
                        // avoid mapping of properties here
                        RelatedProductDef = proddef
                    }
                        ).ToList();

    return products;
}

With my limited knowledge I am stuck here. 以我有限的知识,我被困在这里。

Please help! 请帮忙!

Thanks in advance! 提前致谢!

You can do something like this: 您可以执行以下操作:

var query = (from p in db.ic_ProductDatas
                join proddef in db.ic_ProductDefs on p.ProductDefId equals proddef.ProductDefId
                select new 
                {
                    ProductData = p,
                    Def = proddef
                }).ToList();

List<ic_ProductData> products = new List<ic_ProductData>();
foreach( var product in query)
{
  product.ProductData.RelatedProductDef = product.Def;
  products.Add(product);
}

Basicly, you first need to do the one query to the database, this returns an anonymous type containing both your product and its Def. 基本上,您首先需要对数据库进行一次查询,这将返回一个匿名类型,其中包含您的产品及其Def。 Finally, you loop (in memory, no db-calls!) over these, creating your final objects with their RelatedProductDef properties populated. 最后,您在这些对象上循环(在内存中,没有数据库调用!),并使用RelatedProductDef属性填充最终的对象。

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

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