0

I am a little weak in LINQ to SQL so will try to explain my problem.

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

The ic_ProductData class contains a manually created property as:

public ic_ProductDef RelatedProductDef { get; set; }

I want to modify the above LINQ to SQL query so that I can populate this property.

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

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!

1 Answer 1

2

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. Finally, you loop (in memory, no db-calls!) over these, creating your final objects with their RelatedProductDef properties populated.

Sign up to request clarification or add additional context in comments.

3 Comments

.ToList() is unnecessary as the statement returns an System.Linq.IQueryable<> which can be used in the foreach loop instead
@ViRuSTriNiTy it is indeed unnecessary, I do find its impact (in performance) to be minimal however.
@Tom Wuyts This seems to be a good solution. Will give it a shot! Will update you on this soon. Up vote from me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.