开发者

Finding data in a collection of objects where values are common to all names within the object

I have a collection of objects that contain names, locations, and values. I have a list of locations I need to compare the data to. What I need to do is only pull the records that have names and values that are common to all the locations in my list.

So if there is no item in my objects 开发者_Python百科for one of the locations, I ignore all those items. I only want those items that are all common to my list of locations.

How would I write a linq statement to pull that out? Or even a lambda statement would work. I really don't want to loop through all the records multiple times to find the common records.


If I assume correctly you have an object that has name, location and value as properties and you want to get the ones from a collection with the same location.

Say you have the following object

public class MyObject
{
    public string name{set;get;}
    public string location{set;get;}
    public string value{set;get}
}

and you have a Collection of those objects like IEnumerable<MyObject> myObjects;

You can search through this collection to get the object with the same location:

IEnumerable<MyObject> results = myObjects.Where(mo => mo.location.Contains("targetLocation"));

or you can say:

var results = from o in myObjects 
              where o.location.Contains("something") 
              select c;


This gets all the objects which's location is in the list of locations:

dataObjects.Where(o => locations.Any(l => l == o.Location));

If you need more complex comparisons, you should post the structure of the classes involved, so we can give you a more accurate answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜