Linq并集无法连接2个以上的结果集.只能连接到2个结果集

本文关键字:结果 连接 2个 Linq | 更新日期: 2023-09-27 18:22:02

在我的程序中,我返回3个结果集:

1) resultImportResponse
2) resultUploadRespone
3) 结果附加

我正在检查,如果它们都不为null,我想将它们连接到一个结果集中。

所以我已经申报了var result = (dynamic)null;

在我组合两个结果集中的任何一个之前,它都可以正常工作。

但如果我做result = resultImportResponse.Union(resultAttach); //this works fine

但是说无论我得到什么结果,如果我试图附加第三个结果集,它会给出错误

result = resultImportResponse.Union(resultAttach); 
result = result.Union(resultUploadResponse);  //this gives error

参见下方的样本

public JsonResult GetImportExportAttachmentLog(int doccode)
{
    List<RequestDetail> objImportExportLogs = null;
    objExportLogs = objProxyDownloadManager.GetDownloadRequestDetail(objRequestDetail);
    List<UploadRequest> objImportLogs = null;
    objImportLogs = objProxyUploadManager.GetUploadRequestDetail(objUploadRequestDetail);
    var resultImportResponse = (from i in objExportLogs
                                orderby i.RequestTime descending
                                select new
                                {
                                    Status = i.DownloadStatus.ToString(),
                                    ErrorMessage = i.ErrorLogDescription,
                                    FileId = i.FileId,
                                    FileUri = i.FileUri,
                                    FileName = i.FileName,
                                    IsDownload = i.DownloadStatus.ToString().TrimEnd() == "Complete" ? true : false
                                });
    var resultUploadResponse = (from i in objImportLogs
                                orderby i.RequestStartTime descending
                                select new
                                {
                                    Status = i.UploadStatus.ToString(),
                                    ErrorMessage = i.ErrorLogDescription,
                                    FileId = i.FileId,
                                    FileUri = i.FileUri,
                                    FileName = i.FileName,
                                    IsUpload = i.UploadStatus.ToString().TrimEnd() == "Complete" ? true : false
                                });
    //For Attachment,Start Here
    List<RequestDetail> objAttachImportExportLogs = null;
    objAttachLogs = objProxyDownloadManager.GetDownloadRequestDetail(objAttachRequestDetail);
    var resultAttach = (from i in objAttachImportExportLogs
                        orderby i.RequestTime descending
                        select new
                        {
                            Status = i.DownloadStatus.ToString(),
                            ErrorMessage = i.ErrorLogDescription,
                            FileId = i.FileId,
                            FileUri = i.FileUri,
                            FileName = i.FileName,
                            IsDownload = i.DownloadStatus.ToString().TrimEnd() == "Complete" ? true : false
                        });
    //For Attachment,Ends Here
    var result = (dynamic)null;

    if ((!Equals(resultImportResponse, null) && !Equals(resultAttach, null) && !Equals(resultUploadResponse, null)) && (resultImportResponse.Count() > 0 && resultAttach.Count() > 0 && resultUploadResponse.Count() > 0))
    {
        result = resultImportResponse.Union(resultAttach);
        // result = result.Union(resultUploadResponse);
        result = Enumerable.Union(result, resultUploadResponse);
    }
    else if (!Equals(resultImportResponse, null) && resultImportResponse.Count() > 0)
    {
        result = resultResponse;
    }
    else if (!Equals(resultAttach, null) && resultAttach.Count() > 0)
    {
        result = resultAttach;
    }
    else if (!Equals(resultUploadResponse, null) && resultUploadResponse.Count() > 0)
    {
        result = resultUploadResponse;
    }
    return Json(result);
    }

Linq并集无法连接2个以上的结果集.只能连接到2个结果集

我相信这是因为你试图联合的两个对象是同一个对象(或者至少它们都是带有字段Status, ErrorMessage, FileId, FileUri, FileName, IsDownload的匿名类型)-我现在无法测试这一点,所以我可能错了,但我有理由相信是这样。另一方面,您的resultUploadResponse是不同类型的(井包含IsUpload字段而不是IsDownload字段。)

要解决此问题,需要所有IEnumerable都属于同一类。。所以创建一些类,例如:

public class Result {
    public string Status;
    public string ErrorMessage;
    public string FileId; //Maybe int? can't tell from your code
    public string FileUri;
    public string FileName;
    public bool IsDownload;
    public bool IsUpload;
}

然后你的LINQ应该是,例如:

var resultUploadResponse = (from i in objImportLogs
                            orderby i.RequestStartTime descending
                            select new Result
                            {
                                Status = i.UploadStatus.ToString(),
                                ErrorMessage = i.ErrorLogDescription,
                                FileId = i.FileId,
                                FileUri = i.FileUri,
                                FileName = i.FileName,
                                IsUpload = i.UploadStatus.ToString().TrimEnd() == "Complete";
                            });

并对所有人进行类似的改变:)。

此外,虽然在这种情况下,UnionConcatLINQ语句将执行相同的操作,但从概念上讲,您正在执行Concat,因此可能应该使用该函数:)。

或者:

当您想要使用dynamics时,您可以对动力学执行同样的操作,但单个元素需要是动态的,而不是result变量,因此如果您想避免强烈键入Result类,可以执行:

var resultUploadResponse = (from i in objImportLogs
                            orderby i.RequestStartTime descending
                            select (dynamic) new
                            {
                                Status = i.UploadStatus.ToString(),
                                ErrorMessage = i.ErrorLogDescription,
                                FileId = i.FileId,
                                FileUri = i.FileUri,
                                FileName = i.FileName,
                                IsUpload = i.UploadStatus.ToString().TrimEnd() == "Complete";
                            });

并且对于其它的类似。