简体   繁体   English

如何设置列表对象C#

[英]How to set list object c#

i'm using two list as methods paremeters on a controller but right now i'm getting a problem... Per example, i have the first list with three objects and the second list with three objects too. 我在控制器上使用两个列表作为方法参数,但是现在我遇到了问题...例如,我有第一个包含三个对象的列表,第二个包含三个对象的列表。 Right know i just want to get per each object from the first list the corresponding object from the second list.. like [0]-[0];[1]-[1];[2]-[2]... 知道吧,我只是想从第一个列表中获取每个对象,从第二个列表中获取对应的对象..例如[0]-[0]; [1]-[1]; [2]-[2] ...

I use two foreach to iterate each list but the problem cames when after i go to the second object from Turno list because after that when i start the iteration of DocenteId List the iteration starts at the [0] object but in the reality i don't want the first object of DocenteId list again but the second... 我使用两个foreach来迭代每个列表,但是当我从Turno列表转到第二个对象后出现了问题,因为在此之后,当我开始DocenteId List的迭代时,迭代从[0]对象开始,但实际上我不这样做想要再次列出DocenteId的第一个对象,但第二个...

The if clause that i have is just because i cannot repeat the TurnoId value that cames with the Turno list object... The real thing happens when the DocenteId list cames with same values like object[0]-1;object[1]-1;object[1]-2; 我拥有的if子句只是因为我无法重复Turno列表对象附带的TurnoId值...当DocenteId列表带有相同的值(如object [0] -1; object [1]-)时,就会发生真正的事情1;对象[1] -2; I want this to happen but i just want to get three values as result on the turnodocente List so here is the problem i just can switch the Turno object but not the DocenteId object... What happens with my method right now is that the result came with three values but foreach Turno object i get always the first object value for DocenteId list and i don't want that.... 我希望发生这种情况,但我只想在turnodocente列表上获得三个值,所以这是我只能切换Turno对象而不是DocenteId对象的问题。我的方法现在发生的情况是结果带有三个值,但是foreach Turno对象我总是得到DocenteId列表的第一个对象值,而我不想要那个。

Exists some way to get this? 存在某种方式来做到这一点?

I will apreciate your help... 我会感谢您的帮助...

Controller method 控制器方式

foreach (var item in Turno)
{
    foreach (var ite in DocenteId)
    {
        if (!turnodocente.Any(x =>x.TurnoId == item.TurnoId))
        {
            turnodocente.Add(new TurnoDocente
            {
                TurnoId = item.TurnoId,
                DocenteId = ite
            });                             
        }
    }
}

I don't know if i fully understand your problem but, if both lists have the same number of objects, i think the solution could be like: 我不知道我是否完全了解您的问题,但是,如果两个列表都具有相同数量的对象,我认为解决方案可能是这样的:

for(int i=0; i<Turno.Count(); i++)
{
    turnodocente.Add(new TurnoDocente
        {
            TurnoId = Turno[i],
            DocenteId = DocenteId[i]
        });  
}

Let me know if it helps you. 让我知道是否有帮助。

EDIT: I just tought it might be good to explain. 编辑:我只是想解释一下可能会很好。 When you use foreach in another foreach, the iteration of indexes looks like this: 0 - 0, 0 - 1, 0 - 2... 1 - 0, 1 - 1, 1 - 2... etc so basicaly there is a * a iterations. 当您在另一个foreach中使用foreach时,索引的迭代如下所示:0-0、0-1、0-2 ... 1-0、1-1、1-2 ...等,因此基本上有一个*一次迭代。 What you need is just one iteration through both lists, so "for" loop is a good option because you will get objects from both lists that are at the same index. 您只需要在两个列表中进行一次迭代,因此“ for”循环是一个不错的选择,因为您将从两个具有相同索引的列表中获取对象。 I hope it's clrear :) 我希望它很聪明:)

Considering you have the same number of objects in both lists; 考虑到两个列表中的对象数量相同;

   var Turno = new List<int>() {1000, 2000, 3000};
        var DocenteId = new List<int> {5000, 6000, 7000};

        var turnodocente = new List<KeyValuePair<int, int>>();

        for (int i = 0; i < Turno.Count; i++)
        {
            if (turnodocente.All(x => x.Key != Turno[i]))
            {
                turnodocente.Add(new KeyValuePair<int, int>(Turno[i], DocenteId[i]));
                Console.WriteLine(turnodocente[i].Key +" " + turnodocente[i].Value);
            }
        }

Sounds like you might need Enumerable.Zip 听起来您可能需要Enumerable.Zip

List<int> turno = new List<int>() {1, 2, 3};
List<string> docenteId = new List<string> {"foo", "bar", "doe"};

IEnumerable<TurnoDocente> turnodocente = turno.Zip(docenteId, (x, y) => new TurnoDocente() {TurnoId = x, DocenteId = y} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM