我无法获得通用列表查询的结果

I can't get the result of a Generic List query

public class UTCTime
{
    public DateTime UTCBas { get; set; }
    public DateTime UTCSon { get; set; }
    public TimeSpan Fark { get; set; }
    public char Sezon { get; set; }

    public UTCTime(string UTCStart , string UTCStop , string Fark , char Sezon)
    {
        this.UTCBas = Convert.ToDateTime(UTCStart);
        this.UTCBas = Convert.ToDateTime(UTCStop);
        this.Fark = TimeSpan.Parse(Fark);
        this.Sezon = Sezon;
    }

    public override string ToString()
    {
        return $"{this.UTCBas.ToString(@"yyy-MM-dd")} - {this.UTCBas.ToString(@"yyy-MM-dd")} - {this.Fark} - {this.Sezon}";
    }
}

我正在将来自 class 的数据输入通用列表。

public static void MTDoldur()
{
    MarketTime.Add(new UTCTime("2000-04-01", "2000-10-28", "03:00", 'Y'));
    MarketTime.Add(new UTCTime("2000-10-28", "2001-03-31", "02:00", 'K'));
    MarketTime.Add(new UTCTime("2001-03-31", "2001-10-27", "03:00", 'Y'));
    MarketTime.Add(new UTCTime("2001-10-27", "2002-04-06", "02:00", 'K'));
    MarketTime.Add(new UTCTime("2002-04-06", "2002-10-26", "03:00", 'Y'));
    MarketTime.Add(new UTCTime("2002-10-26", "2003-04-05", "02:00", 'K'));
    MarketTime.Add(new UTCTime("2003-04-05", "2003-10-25", "03:00", 'Y'));
    MarketTime.Add(new UTCTime("2003-10-25", "2004-04-03", "02:00", 'K'));
}

我想将 LINQ 查询的结果添加到将从该类创建的通用列表中。

public class SonucTime
{
    public TimeSpan Fark { get; set; }
    public char Sezon { get; set; }

    public SonucTime(TimeSpan Fark , char Sezon)
    {
        this.Fark = Fark;
        this.Sezon = Sezon;
    }

    public override string ToString()
    {
        return $"Fark : {this.Fark}   Sezon : {this.Sezon}";
    }
}

我在VAR数据类型中拉取查询类型没有失败,但是我无法在屏幕上打印数据并将其分配给变量。 我想将结果传输到通用列表并将数据导出到 VAR 数据类型的变量。 我无法弄清楚我的问题,我在等待你的帮助。

static void Main(string[] args)
{
    MTDoldur();

    DateTime xT = new DateTime();
    xT = new DateTime(2003, 12, 21);

    var sonuc = (from m in MarketTime 
                 where xT >= m.UTCBas && xT <= m.UTCSon
                 select new { m.Fark, m.Sezon }).ToList(); 
}

您在 UTCTime-constructor 中犯了 'copy-past' 错误 - UTCSon 未初始化。

需要修复构造函数:

public UTCTime(string UTCStart , string UTCStop , string Fark , char Sezon)
{
    this.UTCBas = Convert.ToDateTime(UTCStart);
    this.UTCSon = Convert.ToDateTime(UTCStop); // <----
    this.Fark = TimeSpan.Parse(Fark);
    this.Sezon = Sezon;
}

并请求数据:

var sonuc = (from m in MarketTime 
            where xT >= m.UTCBas && xT <= m.UTCSon
            select new SonucTime(m.Fark, m.Sezon))
            .ToList();
/*
result:
sonuc
Count = 1
[0] [SonucTime]:{Fark : 02:00:00   Sezon : K}
Fark [TimeSpan]:{02:00:00}
Sezon [char]:75 'K'
*/