C# 如何将作为列表数组的对象成员的值分配给另一个对象成员

C# 如何将作为列表数组的对象成员的值分配给另一个对象成员,c#,C#,我在这里定义了两个类: public class datatype1 { public double lat { get; set; } public double lon { get; set; } } public class datatype2 { public List<List<double>> lat { get; set; } public List<List<double>> lon { get; s

我在这里定义了两个类:

public class datatype1
{
    public double lat { get; set; }
    public double lon { get; set; }
}
public class datatype2
{
    public List<List<double>> lat { get; set; }
    public List<List<double>> lon { get; set; }
}

var data1=new datatype1();
var data2=new datatype2();
data2.lat[0][0]=30.0;
data2.lat[1][0]=40.0;
data2.lon[0][0]=30.0;
data2.lon[1][0]=40.0;
这个函数使用返回类似datatype2的json类型

(二)

公共类根对象
{
公共列表lat{get;set;}
公共列表lon{get;set;}
公共列表BT900{get;set;}
公共列表BT2500{get;set;}
公共列表BT1030{get;set;}
公共列表BT1500{get;set;}
公共列表iRad900{get;set;}
公共列表iRad2500{get;set;}
全局公共列表QFLW{get;set;}
全局公共列表qfmw{get;set;}
全局公共列表QFSW{get;set;}
公共列表FOV{get;set;}
{get;set;}的公共列表
公共列表扫描{get;set;}
公共列表QF11{get;set;}
公共列表QF12{get;set;}
公共列表QF13{get;set;}
公共列表QF14{get;set;}
公共列表QF15{get;set;}
公共列表QF16{get;set;}
公共列表QF2lw{get;set;}
公共列表QF2mw{get;set;}
公共列表QF2sw{get;set;}
公共列表QF32lw{get;set;}
公共列表QF32mw{get;set;}
公共列表QF32sw{get;set;}
公共列表QF33lw{get;set;}
公共列表QF33mw{get;set;}
公共列表QF33sw{get;set;}
公共列表QF34lw{get;set;}
公共列表QF34mw{get;set;}
公共列表QF34sw{get;set;}
公共列表QF35lw{get;set;}
公共列表QF35mw{get;set;}
公共列表QF35sw{get;set;}
公共列表QF41lw{get;set;}
公共列表QF41mw{get;set;}
公共列表QF41sw{get;set;}
公共列表QF42lw{get;set;}
公共列表QF42mw{get;set;}
公共列表QF42sw{get;set;}
公共列表QF43lw{get;set;}
公共列表QF43mw{get;set;}
公共列表QF43sw{get;set;}
公共列表QF44lw{get;set;}
公共列表QF44mw{get;set;}
公共列表QF44sw{get;set;}
公共列表QF45lw{get;set;}
公共列表QF45mw{get;set;}
公共列表QF45sw{get;set;}
公共列表DSWSizeForwardLw{get;set;}
公共列表DSWSizeForwardMw{get;set;}
公共列表DSWSizeForwardSw{get;set;}
公共列表DSWSizeReverseLw{get;set;}
公共列表DSWSizeReverseMw{get;set;}
公共列表DSWSizeReverseSw{get;set;}
公共列表ITWSizeForwardLw{get;set;}
公共列表ITWSizeForwardMw{get;set;}
公共列表ITWSizeForwardSw{get;set;}
公共列表ITWSizeReverseLw{get;set;}
公共列表ITWSizeReverseMw{get;set;}
公共列表ITWSizeReverseSw{get;set;}
公共列表PRTTempsForward{get;set;}
公共列表PRTTempsReverse{get;set;}
公共列表PulseNoiselW{get;set;}
公共列表脉冲ISEMW{get;set;}
公共列表脉冲ENOISESW{get;set;}
公共列表ZPDFClw{get;set;}
公共列表ZPDFCmw{get;set;}
公共列表ZPDFCsw{get;set;}
公共列表SDRFClw{get;set;}
公共列表SDRFCmw{get;set;}
公共列表SDRFCsw{get;set;}
公共列表ZPDMglw{get;set;}
公共列表ZPDMgmw{get;set;}
公共列表ZPDMgsw{get;set;}
公共列表DSSymmetrylw{get;set;}
公共列表DSSymmetrymw{get;set;}
公共列表DSSymmetrysw{get;set;}
公共列表DSSpcStabForwardlw{get;set;}
公共列表DSSpcStabForwardmw{get;set;}
公共列表DSSpcStabForwardsw{get;set;}
公共列表DSSpcStabReverselw{get;set;}
公共列表DSSpcStabReversemw{get;set;}
公共列表DSSpcStabReversesw{get;set;}
公共列表ITSpcStabForwardlw{get;set;}
公共列表ITSpcStabForwardmw{get;set;}
公共列表ITSpcStabForwardsw{get;set;}
公共列表ITSpcStabReverselw{get;set;}
公共列表ITSpcStabReversemw{get;set;}
公共列表ITSpcStabReversesw{get;set;}
公共列表{get;set;}
公共列表{get;set;}
公共列表{get;set;}
公共列表MeaLaserWL{get;set;}
公共列表MonLaserWL{get;set;}
公共列表ResLaserWL{get;set;}
公共列表{get;set;}
公共列表V{get;set;}
公共列表granuleID{get;set;}
}
在这种特定情况下,返回的对象包含每个属性的11个元素

如何使用foreach循环将datatype2中每个属性的第一个元素分配给datatype1?

datatype2 data2=GetYourJsonData();

对于(var i=0;i您是否正在尝试为每个lat创建新的数据类型1),因此,lat[0][0]将为1,lat[1][0]将是第二个datatype1,或者你到底想做什么?为什么datatype2的lat和长成员列表而不仅仅是简单的列表?我在我的问题中添加了一个特定的案例。请检查它只是为了确认:你是否将
datatype2
作为单个或多个
\u CRIS.SDR.json
?也就是说,一个
datatype2
一个
\u CRIS.SDR.json
或一个
datatype2
用于所有
\u CRIS.SDR.json
?这也是我当前使用的方法。在一个接一个地分配属性值时,它会导致一个非常长的代码列表。在这种情况下,实际上有100个属性。我正在寻找一个方法在foreach循环中执行此操作。我被卡住了,因为datatype2中的属性都是列表。处理类对象中的列表数组不是很简单。我试图从这里寻找解决方案抱歉,我不明白。你的问题不清楚。你希望foreach避免什么?你有两种不同的类型,你希望如何避免赋值对于每个属性??唯一的方法是反射或某些映射工具(实际上使用反射)。是的,我们在讨论反射时已经接近尾声。我之所以不想逐个编写赋值行,是因为这两个类中的属性具有相同的名称。我想可能有
public class RootObject
{
    public double lat { get; set; }
    public double lon { get; set; }
    public int BT900 { get; set; }
    public int BT2500 { get; set; }
    public int BT1030 { get; set; }
    public int BT1500 { get; set; }
    public double iRad900 { get; set; }
    public double iRad2500 { get; set; }
    public int OverallQFlw { get; set; }
    public int OverallQFmw { get; set; }
    public int OverallQFsw { get; set; }
    public int FOV { get; set; }
    public int FOR { get; set; }
    public int Scan { get; set; }
    public int QF11 { get; set; }
    public int QF12 { get; set; }
    public int QF13 { get; set; }
    public int QF14 { get; set; }
    public int QF15 { get; set; }
    public int QF16 { get; set; }
    public int QF2lw { get; set; }
    public int QF2mw { get; set; }
    public int QF2sw { get; set; }
    public int QF32lw { get; set; }
    public int QF32mw { get; set; }
    public int QF32sw { get; set; }
    public int QF33lw { get; set; }
    public int QF33mw { get; set; }
    public int QF33sw { get; set; }
    public int QF34lw { get; set; }
    public int QF34mw { get; set; }
    public int QF34sw { get; set; }
    public int QF35lw { get; set; }
    public int QF35mw { get; set; }
    public int QF35sw { get; set; }
    public int QF41lw { get; set; }
    public int QF41mw { get; set; }
    public int QF41sw { get; set; }
    public int QF42lw { get; set; }
    public int QF42mw { get; set; }
    public int QF42sw { get; set; }
    public int QF43lw { get; set; }
    public int QF43mw { get; set; }
    public int QF43sw { get; set; }
    public int QF44lw { get; set; }
    public int QF44mw { get; set; }
    public int QF44sw { get; set; }
    public int QF45lw { get; set; }
    public int QF45mw { get; set; }
    public int QF45sw { get; set; }
    public int DSWSizeForwardLw { get; set; }
    public int DSWSizeForwardMw { get; set; }
    public int DSWSizeForwardSw { get; set; }
    public int DSWSizeReverseLw { get; set; }
    public int DSWSizeReverseMw { get; set; }
    public int DSWSizeReverseSw { get; set; }
    public int ITWSizeForwardLw { get; set; }
    public int ITWSizeForwardMw { get; set; }
    public int ITWSizeForwardSw { get; set; }
    public int ITWSizeReverseLw { get; set; }
    public int ITWSizeReverseMw { get; set; }
    public int ITWSizeReverseSw { get; set; }
    public int PRTTempsForward { get; set; }
    public int PRTTempsReverse { get; set; }
    public int ImpulseNoiselw { get; set; }
    public int ImpulseNoisemw { get; set; }
    public int ImpulseNoisesw { get; set; }
    public int ZPDFClw { get; set; }
    public int ZPDFCmw { get; set; }
    public int ZPDFCsw { get; set; }
    public int SDRFClw { get; set; }
    public int SDRFCmw { get; set; }
    public int SDRFCsw { get; set; }
    public int ZPDMglw { get; set; }
    public int ZPDMgmw { get; set; }
    public int ZPDMgsw { get; set; }
    public double DSSymmetrylw { get; set; }
    public double DSSymmetrymw { get; set; }
    public double DSSymmetrysw { get; set; }
    public double DSSpcStabForwardlw { get; set; }
    public double DSSpcStabForwardmw { get; set; }
    public double DSSpcStabForwardsw { get; set; }
    public double DSSpcStabReverselw { get; set; }
    public double DSSpcStabReversemw { get; set; }
    public double DSSpcStabReversesw { get; set; }
    public double ITSpcStabForwardlw { get; set; }
    public double ITSpcStabForwardmw { get; set; }
    public double ITSpcStabForwardsw { get; set; }
    public double ITSpcStabReverselw { get; set; }
    public double ITSpcStabReversemw { get; set; }
    public double ITSpcStabReversesw { get; set; }
    public double ITTempStabForward { get; set; }
    public double ITTempStabReverse { get; set; }
    public double ITTempCons { get; set; }
    public double MeaLaserWL { get; set; }
    public double MonLaserWL { get; set; }
    public double ResLaserWL { get; set; }
    public string Maneuver { get; set; }
    public string V { get; set; }
    public string granuleID { get; set; }
}
public class RootObject
{
    public List<List<double>> lat { get; set; }
    public List<List<double>> lon { get; set; }
    public List<List<int>> BT900 { get; set; }
    public List<List<int>> BT2500 { get; set; }
    public List<List<int>> BT1030 { get; set; }
    public List<List<int>> BT1500 { get; set; }
    public List<List<double>> iRad900 { get; set; }
    public List<List<double>> iRad2500 { get; set; }
    public List<List<int>> OverallQFlw { get; set; }
    public List<List<int>> OverallQFmw { get; set; }
    public List<List<int>> OverallQFsw { get; set; }
    public List<List<int>> FOV { get; set; }
    public List<List<int>> FOR { get; set; }
    public List<List<int>> Scan { get; set; }
    public List<List<int>> QF11 { get; set; }
    public List<List<int>> QF12 { get; set; }
    public List<List<int>> QF13 { get; set; }
    public List<List<int>> QF14 { get; set; }
    public List<List<int>> QF15 { get; set; }
    public List<List<int>> QF16 { get; set; }
    public List<List<int>> QF2lw { get; set; }
    public List<List<int>> QF2mw { get; set; }
    public List<List<int>> QF2sw { get; set; }
    public List<List<int>> QF32lw { get; set; }
    public List<List<int>> QF32mw { get; set; }
    public List<List<int>> QF32sw { get; set; }
    public List<List<int>> QF33lw { get; set; }
    public List<List<int>> QF33mw { get; set; }
    public List<List<int>> QF33sw { get; set; }
    public List<List<int>> QF34lw { get; set; }
    public List<List<int>> QF34mw { get; set; }
    public List<List<int>> QF34sw { get; set; }
    public List<List<int>> QF35lw { get; set; }
    public List<List<int>> QF35mw { get; set; }
    public List<List<int>> QF35sw { get; set; }
    public List<List<int>> QF41lw { get; set; }
    public List<List<int>> QF41mw { get; set; }
    public List<List<int>> QF41sw { get; set; }
    public List<List<int>> QF42lw { get; set; }
    public List<List<int>> QF42mw { get; set; }
    public List<List<int>> QF42sw { get; set; }
    public List<List<int>> QF43lw { get; set; }
    public List<List<int>> QF43mw { get; set; }
    public List<List<int>> QF43sw { get; set; }
    public List<List<int>> QF44lw { get; set; }
    public List<List<int>> QF44mw { get; set; }
    public List<List<int>> QF44sw { get; set; }
    public List<List<int>> QF45lw { get; set; }
    public List<List<int>> QF45mw { get; set; }
    public List<List<int>> QF45sw { get; set; }
    public List<List<int>> DSWSizeForwardLw { get; set; }
    public List<List<int>> DSWSizeForwardMw { get; set; }
    public List<List<int>> DSWSizeForwardSw { get; set; }
    public List<List<int>> DSWSizeReverseLw { get; set; }
    public List<List<int>> DSWSizeReverseMw { get; set; }
    public List<List<int>> DSWSizeReverseSw { get; set; }
    public List<List<int>> ITWSizeForwardLw { get; set; }
    public List<List<int>> ITWSizeForwardMw { get; set; }
    public List<List<int>> ITWSizeForwardSw { get; set; }
    public List<List<int>> ITWSizeReverseLw { get; set; }
    public List<List<int>> ITWSizeReverseMw { get; set; }
    public List<List<int>> ITWSizeReverseSw { get; set; }
    public List<List<int>> PRTTempsForward { get; set; }
    public List<List<int>> PRTTempsReverse { get; set; }
    public List<List<int>> ImpulseNoiselw { get; set; }
    public List<List<int>> ImpulseNoisemw { get; set; }
    public List<List<int>> ImpulseNoisesw { get; set; }
    public List<List<int>> ZPDFClw { get; set; }
    public List<List<int>> ZPDFCmw { get; set; }
    public List<List<int>> ZPDFCsw { get; set; }
    public List<List<int>> SDRFClw { get; set; }
    public List<List<int>> SDRFCmw { get; set; }
    public List<List<int>> SDRFCsw { get; set; }
    public List<List<int>> ZPDMglw { get; set; }
    public List<List<int>> ZPDMgmw { get; set; }
    public List<List<int>> ZPDMgsw { get; set; }
    public List<List<double>> DSSymmetrylw { get; set; }
    public List<List<double>> DSSymmetrymw { get; set; }
    public List<List<double>> DSSymmetrysw { get; set; }
    public List<List<double>> DSSpcStabForwardlw { get; set; }
    public List<List<double>> DSSpcStabForwardmw { get; set; }
    public List<List<double>> DSSpcStabForwardsw { get; set; }
    public List<List<double>> DSSpcStabReverselw { get; set; }
    public List<List<double>> DSSpcStabReversemw { get; set; }
    public List<List<double>> DSSpcStabReversesw { get; set; }
    public List<List<double>> ITSpcStabForwardlw { get; set; }
    public List<List<double>> ITSpcStabForwardmw { get; set; }
    public List<List<double>> ITSpcStabForwardsw { get; set; }
    public List<List<double>> ITSpcStabReverselw { get; set; }
    public List<List<double>> ITSpcStabReversemw { get; set; }
    public List<List<double>> ITSpcStabReversesw { get; set; }
    public List<List<double>> ITTempStabForward { get; set; }
    public List<List<double>> ITTempStabReverse { get; set; }
    public List<List<double>> ITTempCons { get; set; }
    public List<List<double>> MeaLaserWL { get; set; }
    public List<List<double>> MonLaserWL { get; set; }
    public List<List<double>> ResLaserWL { get; set; }
    public List<string> Maneuver { get; set; }
    public List<string> V { get; set; }
    public List<string> granuleID { get; set; }
}
datatype2 data2 = GetYourJsonData();
for(var i=0; i<data2.lat.Count; i++)
{
    datatype1 data1 = new datatype1 
        {
            lat = data2.lat[i][0],
            lon = data.lon[i][0] // remember check for nulls
        };

       // do your stuff with data1
}