Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设置列表的属性<;类别>;在C中#_C#_List_Nested Lists - Fatal编程技术网

C# 如何设置列表的属性<;类别>;在C中#

C# 如何设置列表的属性<;类别>;在C中#,c#,list,nested-lists,C#,List,Nested Lists,我有一个这样定义的类: [DataContract] public class Response2 { [DataMember(Name = "done")] public bool done; [DataMember(Name = "records")] public List<Response3> r3entry; } [DataContract] public class Response3 { [DataMember(Name = "

我有一个这样定义的类:

[DataContract]
public class Response2
{
    [DataMember(Name = "done")]
    public bool done;
    [DataMember(Name = "records")]
    public List<Response3> r3entry;
}

[DataContract]
public class Response3
{
    [DataMember(Name = "Id")]
    public string strId { get; set; }
    [DataMember(Name = "Name")]
    public string strName { get; set; }
}

有什么想法吗?

使用投影进行内部连接:

var list = from l1 in LIST1
    join l2 in LIST2 on l1.ID equals l2.ID
    select new {
        l1.ID,
        l1.Item,
        l1.Name,
        l2.Color
    }
    .ToList()

在不确切知道您的目标是什么的情况下,Zip函数作用于两个列表并返回这两个列表的乘积。您可以在此处了解更多信息:

但实际上,如果你有两张清单,让我们说:

int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };
// The following example concatenates corresponding elements of the
// two input sequences.
var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);
因此,在您的情况下,可能是这样的:

string propertyRequest2 = CreatePropertyRequest2();
Response2 propResponse2 = MakeRequest2(propertyRequest2, sfToken);

List<Response> listAllData = new List<Response>();

foreach (var responseEntry in propResponse2.r3entry)
{
    listAllData.Add(new Response() { strId = responseEntry.strId, strName = responseEntry.strName } );

    // NOTE .strId IS ALWAYS UNIQUE IN BOTH CLASSES
    // - I know this is NOT the right syntax... will fix later.
    Where listAllData.strId = responseEntry.strId
    {
        listAllData.property = propertyResponse2(.strId=responseEntry.strId).property
    }
}
// WE HAVE A LIST OF CLASSES WITH PROPERTIES
// ASSUME PROPERTIES ARE ID, ITEM, NAME
LIST1 = { ("1", "A", "APPLE"), ("2", "B", "BANANA"), ("3", "C", "COCONUT")}

// NOW WE HAVE ANOTHER LIST THAT HAS THE SAME ID BUT DIFF DATA
// ASSUME PROPERTIES ARE ID, COLOR
LIST2 = { ("1", "RED"), ("2", "YELLOW"), ("3", "BROWN) }

// AND THEN I WANT TO CREATE A NEW LIST WITH BOTH SETS OF DATA COMBINED
// ASSUME PROPERTIES ARE ID, ITEM, NAME, COLOR
LIST3 = { ("1", "A", "APPLE", "RED"), ("2", "B", "BANANA"), ("3", "C", "COCONUT", "BROWN") }
Response2[] response2 = //some list
Response3[] response3= // some other list
var response2Andresponse3 = response2.Zip(response3, (res2,res3) => //something you want to do with them
这将使response2的第一个元素与response3的第一个元素配对,依此类推。这里我们还假设它们具有相同的长度,这样就不会有未配对的属性


您必须找到一种方法,将其导出到您可能会发现有用的内容中,但您将有一个列表,其中每个元素对应一个元素。

编译时是否知道这些属性?(我们是否知道最终结果将具有
id
名称
颜色
?)或者它们可以是任何属性?如果是这样,冲突会发生什么情况(两个对象具有相同的属性名称但值不同)?对于
Enumerable.Zip()
,元素的顺序必须相同吗?我也不清楚这在多维数组上是如何工作的……Zip将按顺序将它们压缩在一起,所以您必须事先知道它们的顺序。如果你想要多维性,即三元组和n元组,你只需要把一个拉链链到另一个拉链上。然而,你的例子是类,而不是n维数组,所以我想知道你所说的多维性是什么意思。啊,是的,这就是我想要的是连接。我不知道你可以在C#内这样做。我认为这只是SQL的问题。请查阅一些关于LINQ的教程