迭代通用列表中的记录

发布于 2024-12-11 03:05:14 字数 756 浏览 0 评论 0原文

我有一个列表,并且它使用 linq 从该列表中仅返回一条记录,我想做的是迭代返回的一条记录,因此我将返回的子列表转换为 IEnumerable,但我的 foreach 语句只发生一次当我调试时,我显然看到 IEnumerable 有一个索引,但有几个属性,这就是我试图更改的值

示例:

编辑:

var mylist =MyList.Where(d=>d.Field).First();
foreach( var record in mylist)
{
   foreach( var property in record)
   {
      property=value
   }
   property =value; //this only have to one property in the record
   //bascailly I want to iterate over the nested list in the list
   //essentially every index in the main list is a list itself and
   //I am trying to iterate over that indexed list
}

现在我遇到一个编译问题,指出 myList 不包含 的公共定义GetEnumerator 和我不能简单地将列表转换为 IEnumerable

编辑 2 -

抱歉造成混乱,它从来都不是列表。这是一个 IQuerable 对象,结果就是 mylist,我一直在查看代码运行

I have a list and I have a it returning just one record from that list using linq, what I am trying to do is iterate over that one record returned so I cast the returned sublist into IEnumerable, but the foreach statement I have only happens once and when I debug I obviously see that the IEnumerable has one index, but several properties and that's what values I am trying to change

example:

Edit:

var mylist =MyList.Where(d=>d.Field).First();
foreach( var record in mylist)
{
   foreach( var property in record)
   {
      property=value
   }
   property =value; //this only have to one property in the record
   //bascailly I want to iterate over the nested list in the list
   //essentially every index in the main list is a list itself and
   //I am trying to iterate over that indexed list
}

now I am running into a compile issue stating that myList doesn't contain a public definition for GetEnumerator and I cant simply cast the list to IEnumerable

EDIT 2-

sorry for the confusion, it was never a list. It was an IQuerable of objects is what mylist turns out to be, I was looking at the code run the whole time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹂绝世的画 2024-12-18 03:05:14

你需要 2 个 foreach 因为你有一个 List> 不是吗?

foreach(var element in MyList)
{

    foreach(var property in element)
    {
       //do your thing
    }
}

但您只选择了 MyList 上的 First 元素

You need 2 foreach because you have a List<List<>> don't you?

foreach(var element in MyList)
{

    foreach(var property in element)
    {
       //do your thing
    }
}

But you were only selecting the First element on MyList

一场信仰旅途 2024-12-18 03:05:14

如果你只有一项,为什么需要“循环”它?

var myItem = MyList.Where(d=>d.Field).First();

现在这只是一个项目,而不是 IEnumerable

该物品是什么类型?它只是一些具有属性的类吗?如果你想循环这些,你需要使用反射。

If you only have one item, why do you need to "loop over" it?

var myItem = MyList.Where(d=>d.Field).First();

Now that's just an item, not an IEnumerable.

What type is the item? Is it just some class with properties? If you want to loop through those you'd need to use reflection.

昔日梦未散 2024-12-18 03:05:14

除非我误解了,否则这是嵌套在列表中的场景列表。您正在使用某些条件选择零件列表中的一项,并希望为主列表中该项目的所有子项设置属性。如果是这样,下面的内容将映射您的场景

class B
    {
        public int x;
    }
    class A
    {

        public int b;
        public List<B> lst = new List<B>();
    }

    static void Main(string[] args)
    {
        List<B> Sub = new List<B>() { new B { x = 2 }, new B { x = 3 } };
        List<B> Sub2 = new List<B>() { new B { x = 4 }, new B { x = 6 } };
        List<A> Main = new List<A>() { new A() {b =2, lst = Sub2 }, new A() {b=3 , lst = Sub  } };

        var newlst = Main.Where((x) => x.b == 2).First().lst.Select((y)=> {y.x = 5;return y;});

        foreach (var item in newlst)
        {
            Console.WriteLine("A.lst(x).x = {0}", item.x);
        }



    }         

Unless I'm misunderstanding this is your scenario list nested within a list. You are selecting one item in the parten list using some criteria and wish to set a property on all of the sub items of that item in the main list. If so the following maps you your scenario

class B
    {
        public int x;
    }
    class A
    {

        public int b;
        public List<B> lst = new List<B>();
    }

    static void Main(string[] args)
    {
        List<B> Sub = new List<B>() { new B { x = 2 }, new B { x = 3 } };
        List<B> Sub2 = new List<B>() { new B { x = 4 }, new B { x = 6 } };
        List<A> Main = new List<A>() { new A() {b =2, lst = Sub2 }, new A() {b=3 , lst = Sub  } };

        var newlst = Main.Where((x) => x.b == 2).First().lst.Select((y)=> {y.x = 5;return y;});

        foreach (var item in newlst)
        {
            Console.WriteLine("A.lst(x).x = {0}", item.x);
        }



    }         
话少心凉 2024-12-18 03:05:14

这是能够做到的:

 var mylist =(MYOBJECT)(MyList.Where(d=>d.Field).SingleorDefault());
 PropertyInfo[] myproperties = mylist.GetType().GetProperties();
 foreach( var record in myproperties) { 

      if(record.Name==target) //target is the passed string
 } 

this was able to do it:

 var mylist =(MYOBJECT)(MyList.Where(d=>d.Field).SingleorDefault());
 PropertyInfo[] myproperties = mylist.GetType().GetProperties();
 foreach( var record in myproperties) { 

      if(record.Name==target) //target is the passed string
 } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文