How can I combine two outputs from two Linq queries?

I am trying to combine these two objects, but not completely sure how .. Can you help me combine these two result objects?

 //
 // Create Linq Query for all segments in "CognosSecurity"
 //
 var userListAuthoritative = (from c in ctx.CognosSecurities
                             where (c.SecurityType == 1 || c.SecurityType == 2)
                             select new {c.SecurityType, c.LoginName , c.SecurityName}).Distinct();
 //
 // Create Linq Query for all segments in "CognosSecurity"
 //
 var userListAuthoritative3 = (from c in ctx.CognosSecurities
                              where c.SecurityType == 3 || c.SecurityType == 0
                              select new {c.SecurityType , c.LoginName }).Distinct();

I think I see where to go with it ... but to answer the question of the types of objects int, string, stringfor SecurityType, LoginName, and SecurityName, respectively,

If you are wondering why I broke them like that, this is because I want to ignore one column when doing individual. Here are the SQL queries that I convert to SQL.

  select distinct SecurityType, LoginName, 'Segment'+'-'+SecurityName
FROM [NFPDW].[dbo].[CognosSecurity]
where SecurityType =1

  select distinct SecurityType, LoginName, 'Business Line'+'-'+SecurityName
FROM [NFPDW].[dbo].[CognosSecurity]
where SecurityType =2

   select distinct SecurityType, LoginName, SecurityName
FROM [NFPDW].[dbo].[CognosSecurity]
where SecurityType in (1,2)
+3
source share
5 answers

, ( 3 , - ).

, . userListAuthoritative.concat(userListAuthoritative3 ) , , , , linq, , . , CustomType , select new CustomType{ ... } select().

, select() , CustomType.

EDIT: , , .

+5

, , :

var merged = userListAuthoritative.Concat(userListAuthoritative3).Distinct();

, Q, , , , POCO .

Edit

, , Distinct SecurityName. ?

var userListAuthoritative = from c in ctx.CognosSecurities
                            where new[]{0,1,2,3}.Contains(c.SecurityType)
                            group new {c.SecurityType, c.LoginName, c.SecurityName}
                                by new {c.SecurityType, c.LoginName}
                            select g.FirstOrDefault();
+1

. , , .

userListAuthoritative.Concat(userListAuthoritative3);
0

, , () . , :

var userListAuthoritative = (from c in ctx.CognosSecurities
                         where (c.SecurityType == 1 || c.SecurityType == 2 || c.SecurityType == 3 || c.SecurityType == 0)
                         select new {c.SecurityType, c.LoginName , c.SecurityName}).Distinct();
0

, IEqualityComparer<T> ctx.

var merged = userListAuthoritative.Union(userListAuthoritative3);
0

Source: https://habr.com/ru/post/1768331/


All Articles