如何合并两个 Linq 查询的两个输出?

发布于 2024-09-26 02:16:28 字数 1331 浏览 2 评论 0原文

我正在尝试合并这两个对象,但不完全确定如何合并。你能帮我合并这两个结果对象吗?

 //
 // 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();

我想我知道该怎么做...但是要回答这个问题,对象的类型是 intstringstring分别是 SecurityTypeLoginNameSecurityName

如果您想知道为什么我让它们像这样被破坏,是因为我想在执行时忽略一列一个独特的。以下是我要转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

貪欢 2024-10-03 02:16:28

您无法连接它们,因为类型不同(第一个结果类型有 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 do select new CustomType{ ... } in both queries or postprocess the results using select() 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.

半世蒼涼 2024-10-03 02:16:28

我假设您希望保持结果不同:

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

并且,正如 Mike Q 指出的那样,您需要确保您的类型匹配,可以通过为匿名类型提供相同的签名,或者专门为此目的创建您自己的 POCO 类。

编辑

如果我理解您的编辑,您希望您的 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();

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();
娇女薄笑 2024-10-03 02:16:28

我不太确定合并是什么意思,因为您从每个类型返回不同的(匿名)类型。以下内容是否有理由对您不起作用?

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();

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();
左秋 2024-10-03 02:16:28

编辑:假设它们属于同一类型,但事实并非如此。

userListAuthoritative.Concat(userListAuthoritative3);

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

userListAuthoritative.Concat(userListAuthoritative3);
ぃ双果 2024-10-03 02:16:28

尝试下面的代码,您可能需要在 ctx 类型中实现 IEqualityComparer

var merged = userListAuthoritative.Union(userListAuthoritative3);

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

var merged = userListAuthoritative.Union(userListAuthoritative3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文