根据属性合并两个对象列表

时间:2019-01-23 19:48:50

标签: c# performance

我定义了此类:

public class DocumentSearchResult
    {
        public string RowCount { get; set; }
        public string TotalRows { get; set; }
        public Int64 DocumentQueryExecutionTimeMS { get; set; }
        public string DocumentPath { get; set; }
        public string DocumentName { get; set; }
        public string DocumentTitle { get; set; }
        public string DocumentSize { get; set; }
        public string DocumentAuthor { get; set; }
        public string DocumentDescription { get; set; }
        public string DocumentFileExtension { get; set; }
        public double DocumentRank { get; set; }
        public Int64 DocumentDocId { get; set; }
        public Int64 DocumentWorkId { get; set; }
        public string DocumentWrite { get; set; }
        public string DocumentParentLink { get; set; }
        public string DocumentLastModifiedDate { get; set; }
        public string DocumentFileType { get; set; }
        public string DocumentSummary { get; set; }
        public string DocumentLibrary { get; set; }

        //These next set of properties are used for Viewing the results in embedded or preview
        public string DocumentRedirectedEmbededURL { get; set; }
        public string DocumentRedirectPreviewURL { get; set; }
        public string DocumentRedirectURL { get; set; }
        //These two properties are needed to show how the results were gathered MetaData-Content and the folder path location in Ember of the file
        public string SearchSource { get; set; }
        public string FolderPath { get; set; }

我从两个不同的来源获取数据,并使用适当的信息填充此类。现在,客户希望该选项包括两组数据并返回一个消除重复项的响应,另一个选项是从一个列表中找到另一个列表中的匹配记录,并仅返回两个列表中都出现的项。因此,第一个选项是AND合并,以包括两个列表中的所有结果,但不显示重复项。另一个是OR合并,其中结果必须在两个列表中,以修剪所有不匹配的记录。

因此要合并列表或合并列表,我创建了此功能:

public List<DocumentSearchResult> StitchMetaDataAndContent(List<DocumentSearchResult> metaDataResults, List<DocumentSearchResult> contentResults, bool merge = false)
    {
        if (merge)
        {
            List<DocumentSearchResult> stitchedContent;
            stitchedContent = metaDataResults.Union(contentResults).ToList();
            foreach(DocumentSearchResult docX in stitchedContent)
            {
                docX.RowCount = (metaDataResults.Count + contentResults.Count).ToString();
            }
            return stitchedContent;
        }
        else
        {
           metaDataResults.RemoveAll(x => !contentResults.Any(y => y.DocumentName == x.DocumentName));
            foreach(DocumentSearchResult docX in metaDataResults)
            {
                docX.RowCount = metaDataResults.ToString();
            }
            return metaDataResults;
        }
    }

对于合并部分,我使用Union运算符将内容合并到结果中。这似乎可行,但是我在剥离重复项时遇到问题。我需要基于这两个集合的DocumentName属性剥离重复项。这是记录中唯一保证两组之间相同的数据。

在第二个看来,RemoveAll正在使用DocumentName工作(我将继续进行测试以确保),并剥离metaDataResult列表中不在contentResults列表中的所有文件,但是我担心这会随着列表的增加速度变慢。 metaDataResults可能为10,000,而我知道对contentResults进行的某些搜索可能超过15万个结果。 RemoveAll是最好的方法吗?有人可以向我指出一种更快的方式来提高性能吗?

谢谢。

0 个答案:

没有答案
相关问题