none
Mashing together result sets RRS feed

  • Question

  • I have three result collections coming from a trio of LINQ queries.  They are all pulling info from the same view on the server.  The results return sum'ed sales data, one result set each for Company-wide sales, User's Office sales, User's sales. One line per year.

    Company result set columns: Year, CompanySales

    User's Office result set columns:  Year, User'sOfficeSales

    User result set columns:  Year, User'sSales

    My question is: how do I put these result sets into one resultset with these columns?

    Year, CompanySales, UsersOfficeSales, UsersSales

     

    Thursday, January 13, 2011 4:20 PM

Answers

  • you can run Linq on all of the Three results sets and keep on accumlating them each time in a common result set.

    var

     

     

    list1 = (from s in CompanyResultSet select new { Year = s.ToString(), CompanySales = 0 }).ToList();

     

     

    var list2 = (from s in User's Office result select new { Year = s.ToString(), UsersOfficeSales = 0 }).ToList();

     

     

    var list3 = (from s in User result set  select new { Year = s.ToString(), UsersSales = 0 }).ToList();

     

     

    var FinalList =

    (

     

    from l3 in list3

     

     

    join semi in (from l1 in list1

     

     

    join l2 in list2 on l1.Year equals l2.Year into subset

     

     

    from sub in subset.DefaultIfEmpty()

     

     

    select new { l1.Year, UsersOfficeSales = (sub == null ? 0 : sub.UsersOfficeSales), CompanySales = sub == null ? 0 : l1.CompanySales })

     

     

    on l3.Year equals semi.Year into finalSet

     

     

    from set in finalSet.DefaultIfEmpty()

     

     

    select new { set.Year, UsersOfficeSales = (set == null ? 0 : set.UsersOfficeSales), UsersSales = (set == null ? 0 : l3.UsersSales), CompanySales = (set == null ? 0 : set.CompanySales) }).ToList();

     

     

     


    Nikx Gupta MCP, MCTS
    Friday, January 21, 2011 10:33 AM

All replies

  • Hi,

    Does the Attach() method not work? Did you try it?

    Sunday, January 16, 2011 5:58 AM
  • you can run Linq on all of the Three results sets and keep on accumlating them each time in a common result set.

    var

     

     

    list1 = (from s in CompanyResultSet select new { Year = s.ToString(), CompanySales = 0 }).ToList();

     

     

    var list2 = (from s in User's Office result select new { Year = s.ToString(), UsersOfficeSales = 0 }).ToList();

     

     

    var list3 = (from s in User result set  select new { Year = s.ToString(), UsersSales = 0 }).ToList();

     

     

    var FinalList =

    (

     

    from l3 in list3

     

     

    join semi in (from l1 in list1

     

     

    join l2 in list2 on l1.Year equals l2.Year into subset

     

     

    from sub in subset.DefaultIfEmpty()

     

     

    select new { l1.Year, UsersOfficeSales = (sub == null ? 0 : sub.UsersOfficeSales), CompanySales = sub == null ? 0 : l1.CompanySales })

     

     

    on l3.Year equals semi.Year into finalSet

     

     

    from set in finalSet.DefaultIfEmpty()

     

     

    select new { set.Year, UsersOfficeSales = (set == null ? 0 : set.UsersOfficeSales), UsersSales = (set == null ? 0 : l3.UsersSales), CompanySales = (set == null ? 0 : set.CompanySales) }).ToList();

     

     

     


    Nikx Gupta MCP, MCTS
    Friday, January 21, 2011 10:33 AM
  • Hello Sithlrd,

     

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?  And because your thread has lasted for a long time, I will close it for you by marking an answer.
     
    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 24, 2011 3:32 AM
    Moderator