none
Return sub set of join. RRS feed

  • Question

  • I have a query:

                var subCategoryDefinitions = from cx in catalogContext.CatIDXREFs
                                             join c in catalogContext.BCatalogProducts on cx.CatID.Value.ToString() equals c.CatID into catIdList
                                             select new { cx.CatID, cx.TopLevelCat, cx.SubCat, catIdList };
                foreach (var subCategory in subCategoryDefinitions)
    

    And later almost the same query

                var subCategoryDefinitions = from cx in catalogContext.CatIDXREFs
                                             join c in catalogContext.ACatalogProducts on cx.CatID.Value.ToString() equals c.CatID into catIdList
                                             select new { cx.CatID, cx.TopLevelCat, cx.SubCat, catIdList };
                foreach (var subCategory in subCategoryDefinitions)
    

    The difference is that the first query goes against BCatalogProducts and the second goes against ACatalogProducts. The two tables are almost identical and for my query I would like to consider them identical. The problem is that ACatalogProducts and BCatalogProducts have different columns. I would like to restrict the columns coming back on the join so a small subset of the columns available. I was thinking something like 'into new {. . . .} catIdList' or 'select new {. . . . .} into catIdList'. I am not sure of the exact syntax and trying all the different possibilities was too much like trial and error so I was hoping I could get some assistance from the members of this group.

    Thank you.

    Kevin
    Monday, January 25, 2010 11:49 PM

Answers

  •             var subCategoryDefinitions = from cx in catalogContext.CatIDXREFs

                                             join c in catalogContext.BCatalogProducts on cx.CatID.Value.ToString() equals c.CatID into catIdList

                                             select new { cx.CatID, cx.TopLevelCat, cx.SubCat, catIdList };

               foreach (var subCategory in subCategoryDefinitions)

               var subCategoryDefinitions = from cx in catalogContext.CatIDXREFs

                                             join c in catalogContext.ACatalogProducts on cx.CatID.Value.ToString() equals c.CatID into catIdList

                                             select new { cx.CatID, cx.TopLevelCat, cx.SubCat, catIdList };

                foreach (var subCategory in subCategoryDefinitions)


    Hey kevin,
    you can create a simple class with all the required (superset) properties that you will return from each set

    public class MyEntity
    {
       ...
    }

    then you can assign the return value to the properties and use the linq extension methods to return list

    List<MyEntity> subCategoryDefinitions = (from cx in catalogContext.CatIDXREFs
                                  join c in catalogContext.BCatalogProducts on cx.CatID.Value.ToString() equals c.CatID into catIdList
                                  select new MyEntity() { CatID = cx.CatID, TopLevel = cx.TopLevelCat, SubCat = cx.SubCat }).ToList();

    hope this helps.

    regards,
    kashif
    • Edited by Kashif Pervaiz Tuesday, January 26, 2010 12:02 PM added more text
    • Marked as answer by KevinBurton Tuesday, January 26, 2010 3:23 PM
    Tuesday, January 26, 2010 12:01 PM