【问题标题】:Filter by most reliable datasource using queries in Linq使用 Linq 中的查询按最可靠的数据源过滤
【发布时间】:2012-11-27 15:28:27
【问题描述】:

我对使用 Linq 相当陌生,这对我来说在从 c# 数据表中过滤最佳数据源时有点棘手。我需要对数据表执行以下过滤。数据背后的背景是它是一组记录,其中包含来自不同数据源的独立冗余,以实现故障保护。如果某个数据源在某个时候损坏,则辅助或第三个数据源将成为该事件线程的主要源。

原始数据(例如): 可签名:

source  First       Last
d1      John        Smith
d1      John        Smith
d3      John        Smith
d1      Jane        Doe
d2      Jane        Doe
d3      Richard     Miles
d3      Richard     Miles
d1      Richard     Miles

我想为此添加两列:按(名字、姓氏和数据源)分组的唯一成员计数,以及基于不同组(名字、姓氏、数据源)但按任何数据源排序的唯一记录组编号特定的名字姓氏的记录最多,最少。

source  First   Last    Count   UniqueRecordGroup
d1      John    Smith   2       1
d1      John    Smith   2       1
d3      John    Smith   1       2
d1      Jane    Doe     1       1
d2      Jane    Doe     1       2
d3      Richard Miles   2       1
d3      Richard Miles   2       1
d1      Richard Miles   1       2

然后我最终将只过滤掉主要(唯一记录组 1),以消除该特定记录的冗余/不太可靠的数据源。

source  First   Last    Count   UniqueRecordGroup
d1      John    Smith   2       1
d1      John    Smith   2       1
d1      Jane    Doe     1       1
d3      Richard Miles   2       1
d3      Richard Miles   2       1

如何在数据表(例如数据表 signintable)上使用 Linq 完成上述步骤?

谢谢。

【问题讨论】:

    标签: c# asp.net linq


    【解决方案1】:

    现在完成此应用程序功能后,我不建议尝试为此目的使用 Linq。我发现没有多少资源和论坛专门用于更多涉及的 Linq 查询,而对于 Oracle 或 SQL,至少在需要的部分上有很多论坛和帖子。所以我没有在 Linq 脚本的 c#/asp.net 层中添加该功能,而是在数据库查询层中添加了它。

    在 Oracle 中,我通过 4 个步骤完成了上述操作:

    --1.  creating a derived table of unique instances of main data (a): (First, Last) and a count of instances in the main table:
    
    b As
    (select First, Last, source, COUNT(*) distinctpersoncount
    From a
    GROUP BY First, Last, source)
    
    --2.  Rank the fills from greatest to least.
    
    c As
    (select b.*, row_number() over(partition by  First, Last
    order by distinctpersoncount desc) as distinctpersonrowid
    from b
    
    
    --3.  combine the new distinctperson columns into a joined table with the original data a.
    
    d As
    (select a.*, c.distinctpersoncount, c.distinctpersonrowid
    from a
    left join c
    on
    .... (join conditions here)
    
    --4.  select all the data in d, filtering by distinctpersonrowid = 1 (the best source).
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多