C#无法投射对象

时间:2020-08-07 21:31:22

标签: c# linq

我使用linq按TradingDate对数据进行分组。 statuses.TradingDate.ToString();行导致错误

InvalidCastException: Unable to cast object of type 'FileInsight.Models.RelatedStatuses' to type 'System.String'.

我没有将RelatedStatuses对象转换为字符串,而是将对象内的DateTime属性转换为字符串。我在下面添加了模型和方法

存在转换错误的方法

public async Task<IEnumerable<IGrouping<string, RelatedStatuses>>> GetFilterFileAndRelatedFiles(string searchString)
{
    List<FilterFileAndRelatedFiles> filterFileRelatedFilesList = new List<FilterFileAndRelatedFiles>();
    IEnumerable<FilterFileAndRelatedFiles> filterFileRelatedFiles = await _tradeLogDBContext.GetFilterFileAndRelatedFiles(searchString);

    List<RelatedStatuses> relatedStatuses = new List<RelatedStatuses>();
    foreach(FilterFileAndRelatedFiles ff in filterFileRelatedFiles)
    {
        // Calculates Statuses
        bool DEStatus = ff.DEFileIsValid && (ff.DEFileImportStatus.Equals("SUCCESS"));
        bool VmondagStatus = ff.VmonDagFileExDaStatus.Equals("SUCCESS") && ff.VMonDagFileImportStatus.Equals("SUCCESS") && ff.VMonDagFileParseStatus.Equals("SUCCESS");
        bool ProductXMLStatus = ff.ProductXmlFileImportStatus != null && ff.ProductXmlFileImportStatus.Equals("SUCCESS");
        bool FFStatus = ff.FilterFileImportStatus.Equals("SUCCESS");
        bool DeepDiveStatus = ff.DeepDiveFileImportStatus != null && ff.DeepDiveFileImportStatus.Equals("SUCCESS");
        bool VappFamilyStatus = ff.VAppFamilyXmlFileImportStatus.Equals("SUCCESS");
        bool PlStatus = ff.TradingEventPLFileStatusStatus.Equals("Approved");
        // Created Related Status Object
        var relatedStatus = new RelatedStatuses(ff.filterFileName, DEStatus, VmondagStatus, ProductXMLStatus, FFStatus, DeepDiveStatus, VappFamilyStatus, PlStatus, ff.tradingDate);
        // Add to list
        relatedStatuses.Add(relatedStatus);
    }
    var orderedByDate = from statuses in relatedStatuses
                        group statuses by statuses.TradingDate.ToString();
    return orderedByDate;
}

模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FileInsight.Models
{
    public class RelatedStatuses
    {
        public RelatedStatuses(string name, bool DEStatus, bool VmonDagStatus, bool ProductXMLStatus, bool FFStatus, bool DeepDiveStatus, bool VappFamilyStatus, bool PLFileStatus, DateTime TradingDate)
        {
            this.name = name;
            this.DEStatus = DEStatus;
            this.VmonDagStatus = VmonDagStatus;
            this.ProductXMLStatus = ProductXMLStatus;
            this.FFStatus = FFStatus;
            this.DeepDiveStatus = DeepDiveStatus;
            this.VappFamilyStatus = VappFamilyStatus;
            this.PLFileStatus = PLFileStatus;
            this.TradingDate = TradingDate;
        }
        public string name { get; set; }
        public bool DEStatus { get; set; }
        public bool VmonDagStatus { get; set; }
        public bool ProductXMLStatus { get; set; }
        public bool FFStatus { get; set; }
        public bool DeepDiveStatus { get; set; }
        public bool VappFamilyStatus { get; set; }
        public bool PLFileStatus { get; set; }
        public DateTime TradingDate { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

我能够复制从API控制器运行的异常,并且看来解析器(在我的情况下为JSON解析器)在处理IGrouping时遇到困难。我做了一个简单的更改,将IGrouping接口投影为匿名(dynamic)类型,这使我可以返回列表:

在API控制器中:

public class TestController : ControllerBase
{
   [HttpGet]
   public async Task<IEnumerable<dynamic>> Get()
   {
      IEnumerable<dynamic> relatedStatuses = await GetFilterFileAndRelatedFiles();
      return relatedStatuses;
   }

   private async Task<IEnumerable<dynamic>> GetFilterFileAndRelatedFiles()
   {
      var time1 = DateTime.UtcNow;
      var time2 = time1.AddDays(3);
      var data = new List<RelatedStatuses>() {
         new RelatedStatuses("1", true, true, true, true, true, true, true, time2),
         new RelatedStatuses("2", true, true, true, true, true, true, true, time2),
         new RelatedStatuses("3", true, true, true, true, true, true, true, time1),
         new RelatedStatuses("4", true, true, true, true, true, true, true, time1),
         new RelatedStatuses("5", true, true, true, true, true, true, true, time2),
         new RelatedStatuses("6", true, true, true, true, true, true, true, time2),
         new RelatedStatuses("7", true, true, true, true, true, true, true, time1),
      };
      return await Task.FromResult(
         (from obj in data
            group obj by obj.TradingDate.ToString()).ToList().Select(statusGroup =>
            {
               /*
               * Projecting the IGrouping interface structure in to an anonymous object...
               */
               return new { key = statusGroup.Key, statuses = statusGroup.Select(status => status).ToList() };
            })
      );
   }
}

样本输出:

[
    {
        "key": "08/11/2020 04:34:59",
        "statuses": [
            {
                "name": "1",
                "deStatus": true,
                "vmonDagStatus": true,
                "productXMLStatus": true,
                "ffStatus": true,
                "deepDiveStatus": true,
                "vappFamilyStatus": true,
                "plFileStatus": true,
                "tradingDate": "2020-08-11T04:34:59.3080946Z"
            },
            {
                "name": "2",
                "deStatus": true,
                "vmonDagStatus": true,
                "productXMLStatus": true,
                "ffStatus": true,
                "deepDiveStatus": true,
                "vappFamilyStatus": true,
                "plFileStatus": true,
                "tradingDate": "2020-08-11T04:34:59.3080946Z"
            },
            ...
        ]
    },
    {
        "key": "08/08/2020 04:34:59",
        "statuses": [
            {
                "name": "3",
                "deStatus": true,
                "vmonDagStatus": true,
                "productXMLStatus": true,
                "ffStatus": true,
                "deepDiveStatus": true,
                "vappFamilyStatus": true,
                "plFileStatus": true,
                "tradingDate": "2020-08-08T04:34:59.3080946Z"
            },
            {
                "name": "4",
                "deStatus": true,
                "vmonDagStatus": true,
                "productXMLStatus": true,
                "ffStatus": true,
                "deepDiveStatus": true,
                "vappFamilyStatus": true,
                "plFileStatus": true,
                "tradingDate": "2020-08-08T04:34:59.3080946Z"
            },
            ...
        ]
    },
    ...
]