How to make union on 2 differet var object using LiNQ on C#

Mohammad Qasim 576 Reputation points
2020-09-18T07:46:00.747+00:00

Greetings,
using Sharepoint 2016 On Prem

using LINQ , have retrieved data into 2 different Var objects. (resultstatus ,queryresultfolderAdmin )

Requirement:

i just want to make union/concate ( like Sql server ) between this ( var objects ) to make one, then i will execute on 1 list/var object loop to retrieve data

My code

// List 1 name is "resultstatus "
oQuery.Query = "<Where><Eq><FieldRef Name='Flag'/><Value Type ='number'>" + 1 + "</Value></Eq></Where> ";
var resultstatus = (from SPListItem itm in pLis.GetItems(oQuery)
orderby itm["ID"]
group itm by new { p1 = itm["status"] } into g
select new { m_status = g.Key.p1, m_totaldocs = g.Count() });

// Lisy 2 name is "queryresultfolderAdmin "

var queryresultfolderAdmin = (from af in resultFolderAdmin
join fd in resultfoldeDocAdmin on Convert.ToInt32(af.m_folderidAdmin) equals Convert.ToInt32(fd.m_folderIdAdmin[0])
// where Convert.ToInt32(fh.m_folderIDhier).Equals( 237)
select new { fd.m_totaldocsfoldercountAdmin, fd.m_folderIdAdmin, fd.m_status_folderAdmin });

foreach (var oitmAll in queryresultfolderAdmin)
{

                                oUser.totaldocs += oitmAll.m_totaldocsfoldercountAdmin;
                                switch (Convert.ToInt32(oitmAll.m_status_folderAdmin))
                                {
                                    case 1:
                                        oUser.pendingdocs += Convert.ToInt32(oitmAll.m_totaldocsfoldercountAdmin);
                                        break;

                                    case 2:
                                        oUser.approveddocs += Convert.ToInt32(oitmAll.m_totaldocsfoldercountAdmin);
                                        break;

                                    default:
                                        oUser.rejecteddocs += Convert.ToInt32(oitmAll.m_totaldocsfoldercountAdmin);
                                        break;

                                }

                            }

Solution Required.

How can i do this to transfer All data into 1 list to perform execution into 1 list ( instead of performing execution in 2 list ) like we do in Sql server to make union on 2 different table to get output.

Thanks

Microsoft 365 and Office SharePoint Server Development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Baker Kong-MSFT 3,801 Reputation points
    2020-09-21T03:08:58.577+00:00

    Hi @Mohammad Qasim ,

    If 'm_status_folderAdmin'/'m_status ' is the same property, and you want to perform left outer joins on these 2 lists. You can take a reference of below demo:

    26000-image.png

    If these 2 lists are completely unrelated, please take a reference of 'CrossJoin':

    Besides, you can define a common object type then convert these list elements to this type.

    25988-image.png
    query return:
    26043-image.png

    With the same list type, we can easily use below methods:

    Best Regards,
    Baker Kong


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Mohammad Qasim 576 Reputation points
    2020-09-23T06:13:08.087+00:00

    basically what i want is.

    data on 2 different "var objects", just want to merge data into 1 object to show output .
    it helps me to create loop on 1 "var object" to retrieve data instead 2.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.