Lambda表达问题 [英] Problems with lambda expression

查看:62
本文介绍了Lambda表达问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我对lambda表达式有一些问题.当然,其中一些可以通过foreach循环来完成,但我希望使用合理的简单lambda表达式.

我有以下方法

Hi
I have some problems with a lambda expression. Of course the some could be done with foreach loops but I would prefer reasonably simple lambda expression.

I have a following method

private IEnumerable<IContainer> GetContainers()
{
...
}



如您所见,该方法应返回IContainer实例的集合,其中IContainer是以下接口



as you can see the method should return a collection of IContainer instances where an IContainer is the following interface

public interface IContainer
{
IEnumerable<MyItem> Items{get;}
int Id{get;}
}


在GetContainers()方法内部,我检索了IContainer实例的集合,其中某些容器可以具有相同的ID.我需要一个lambda表达式,该表达式可以将该集合转换为每个容器具有不同ID的集合.如果源集合具有具有相同ID的容器,则目标集合应仅具有一个具有该ID的容器,但其Items属性应包含在具有该ID的容器中找到的所有项目.我希望我的问题描述得足够清楚,可以理解.我可以发布使用foreach循环的现有代码,但是我认为上面的文本更容易理解.任何想法将不胜感激.

Uroš


Inside GetContainers() method I retrive the collection of IContainer instances, where some of the containers can have the same Id. I need a lambda expression which would transform that collection into a collection where each container has a different Id. If the source collection had containers with same Ids, the destination collection should have only one container with that Id, but it''s Items property should contain all items found in containers with that Id. I hope my question is described clearly enough to be understood. I could post the existing code which uses the foreach loops, but I think that the text above is easier to understand. Any ideas will be appreciated.

Uroš

推荐答案

到目前为止,关于如何从数据库等中获取数据,您还没有做太多的事情.

我举了一个简单的示例,该示例应该可以帮助您使用lambda从集合中获得不同的值.

You haven''t given much to go on so far in respect to how your getting the data out of the database etc.

I have made a simple example that should help you get distinct values from the collection using lambda.

List<test> d = new List<test>();

for (int i = 0; i <= 5; i++)
{
  d.Add(new TEst { ID=i, Name="Simon" });
}

d.Add(new TEst { ID = 1, Name = "Simon" });

var x = (from s in d select s).Distinct();





TEst类的定义





Definition for the TEst class

public class TEst
{
 public int ID { get; set; }
 public string Name { get; set; }
}


GetContainers()将需要构造一个新的实现IContainer接口的类实例集合.然后,您可以使用.GroupBy()扩展方法.

这正是您想要的我想的:
GetContainers() will need to construct a new collection of class instances that implement the IContainer interface. Then you can use the .GroupBy() extension method.

This does what I think you are looking for:
// example proxy for IContainer-implementing class
public class TestItem
{
  public List<int> Items;
  public int Id;
}

// example:
List<TestItem> test = new List<TestItem>();
var item1 = new TestItem { Id = 1, Items=new List<int>()};
item1.Items.Add(10);
item1.Items.Add(11);
item1.Items.Add(12);
test.Add(item1);
var item2 = new TestItem { Id = 2, Items=new List<int>()};
item2.Items.Add(20);
item2.Items.Add(21);
item2.Items.Add(22);
test.Add(item2);
var item3 = new TestItem { Id = 1, Items=new List<int>()};
item3.Items.Add(30);
item3.Items.Add(31);
item3.Items.Add(32);
test.Add(item3);

var selected = test.GroupBy(m => m.Id)
                   .Select(g => g.Aggregate(new TestItem() { Id = g.Key, 
                                                             Items = new List<int>() }, 
                                            (agg, item) => { agg.Items.AddRange(item.Items); 
                                                             return agg; }));
Console.WriteLine(selected.Count());


这篇关于Lambda表达问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆