如何合并两个 Linq 查询的两个输出?
我正在尝试合并这两个对象,但不完全确定如何合并。你能帮我合并这两个结果对象吗?
//
// 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();
我想我知道该怎么做...但是要回答这个问题,对象的类型是 int
、string
、string
分别是 SecurityType
、 LoginName
和 SecurityName
如果您想知道为什么我让它们像这样被破坏,是因为我想在执行时忽略一列一个独特的。以下是我要转换为 SQL 的 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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法连接它们,因为类型不同(第一个结果类型有 3 个属性,第二个有两个)。
如果您可以容忍为第二个查询的第三个结果输入空值,这将会有所帮助。然后我建议你只做一个
userListAuthoritative.concat(userListAuthoritative3 )
但是我认为这不会起作用,因为 linq 生成的匿名类型不会属于同一类,即使结构是一样的。要解决这个问题,您可以定义一个 CustomType 来封装元组,并在两个查询中执行select new CustomType{ ... }
操作,或者在一个查询中使用select()
对结果进行后处理。类似的方式。实际上,后一种 select() 方法还允许您通过在 CustomType 的后处理中实现带有 null 的选择来解决参数计数不匹配问题。编辑:根据下面的评论一旦结构相同,匿名类型将相同。
You can't join these because the types are different (first has 3 properties in the resulting type, second has two).
If you can tolerate putting a null value in for the 3rd result of the second query this will help. I would then suggest you just do a
userListAuthoritative.concat(userListAuthoritative3 )
BUT I think this will not work as the anonymous types generated by the linq will not be of the same class, even tho the structure is the same. To solve that you can either define a CustomType to encapsulate the tuple and doselect new CustomType{ ... }
in both queries or postprocess the results usingselect()
in a similar fashion.Acutally the latter select() approach will also allow you to solve the parameter count mismatch by implementing the select with a null in the post-process to CustomType.EDIT: According to the comment below once the structures are the same the anonymous types will be the same.
我假设您希望保持结果不同:
并且,正如 Mike Q 指出的那样,您需要确保您的类型匹配,可以通过为匿名类型提供相同的签名,或者专门为此目的创建您自己的 POCO 类。
编辑
如果我理解您的编辑,您希望您的 Distinct 忽略 SecurityName 列。这是正确的吗?
I assume that you want to keep the results 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?
我不太确定合并是什么意思,因为您从每个类型返回不同的(匿名)类型。以下内容是否有理由对您不起作用?
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?
编辑:假设它们属于同一类型,但事实并非如此。
Edit: This assumed they were of the same type -- but they're not.
尝试下面的代码,您可能需要在 ctx 类型中实现
IEqualityComparer
。Try below code, you might need to implement
IEqualityComparer<T>
in your ctx type.