views:

68

answers:

5

I'm trying to merge these two object but not totally sure how.. Can you help me merge 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 this... but to answer the question the types of the objects are int, string, string for SecurityType, LoginName , and SecurityName respectively

If you're wondering why I have them broken like this is because I want to ignore one column when doing a distinct. Here are the SQL queries that I'm converting 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)
A: 

Edit: This assumed they were of the same type -- but they're not.

userListAuthoritative.Concat(userListAuthoritative3);
Kirk Woll
You can only concat collections of the same type, take a look at what's being returned :)
Nick Craver
@Nick, ah, you're right, they looked the same to me at first glance, but good catch.
Kirk Woll
+4  A: 
Mike Q
If the structure is the same, and they are in the same assembly, then the anonymous types will be the same, and a concat will work.
StriplingWarrior
@StriplingWarrior - That isn't true here, the anonymous types aren't the same...they have a different number of properties...
Nick Craver
I didn't know that, modified to reflect.
Mike Q
@Nick - That's why I said "if the structure is the same." Mike's original response indicated that he didn't think anonymous types would work, even if he made sure that they had the same types and names on their fields.
StriplingWarrior
@Nick I think SW is saying that IF the structure was modified as I suggest in the first paragraph then the concat will work fine.
Mike Q
@StriplingWarrior - Ah, I didn't see the original post then would be the issue :)
Nick Craver
Instead of doing an anonymous type, I created my own type and that seems to work fine. Thank you!
MakerOfThings7
A: 

I assume that you want to keep the results distinct:

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

And, as Mike Q pointed out, you need to make sure that your types match, either by giving the anonymous types the same signature, or by creating your own POCO class specifically for this purpose.

Edit

If I understand your edit, you want your Distinct to ignore the SecurityName column. Is that correct?

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();
StriplingWarrior
I'd like to ignore the securityName column (for distinct) only if SecurityType == 0 or 3 so this may not be the answer...
MakerOfThings7
A: 

I'm not exactly sure what you mean by merge, since you're returning different (anonymous) types from each one. Is there a reason the following doesn't work for you?

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();
Davy8
I can't do this since I need to have distinct work on a different set of objects in each run
MakerOfThings7
A: 

Try below code, you might need to implement IEqualityComparer<T> in your ctx type.

var merged = userListAuthoritative.Union(userListAuthoritative3);
Broomandan