I have two different lists where one is a bunch of ID's as in a List<int> idsList, the other however is a list of objects like List<MyObject> myObjectList where the object looks like this:
class MyObject{
private List<int> ids;
public MyObject(List<int> ids){
this.ids = ids;
}
public List<int> Ids{
get{
return ids;
}
}
}
As you can see each object can contain one or multiple IDs (never zero or null ids). So what I need at the end is to know what objects in myObjectList have any id(s) from my idsList.
So far if I do:
var ids = from g in onScreen where g.Ids.Contains(myIntVariable) select g;
it would give me a list of the object(s) that contain myIntVariable. What I do not know how to do is to match the content of the idsList with the list in MyObject.
Thanks!
One way to go:
var listOfMyObjectsContainingAnIdFromIdsList = myObjectList.Where(myObject => myObject.Ids.Any(id => idsList.Contains(id)));
Assuming g is your object list and idsList is your int list:
foreach (var myObject in g.Where( obj => obj.Ids.Any( itemId => idsList.Contains(itemId) ) )) {
//Use your myObject here
}
Hope it works,
Related
I have a list like this:
public class list
{
public IList<list2> list {get;set;}
}
And a list2 like this:
public class list2
{
public string something {get;set;}
}
The result I want is this:
var listWithAll = new List<list2>();
foreach (var item in list)
{
foreach (var item2 in item.list)
{
listWithAll.Add(item2);
}
}
Is there a short LINQ handler I can use do to this?
Something like this:
list.Select(x => x.list);
But this doesn't work obviously.
Use SelectMany method of LINQ
var newlist = list.SelectMany(x => x.list).ToList();
SelectMany flattens queries that return lists of lists.
For further details refer here
You can use SelectMany for this purpose:
var listWithAll = list.SelectMany(x => x.list).ToList();
The SelectMany:
Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.
I have the following classes:
public class MyItem
{
public string Email;
public int Value1;
public datetime Value2;
//etc
}
public class MyItems : List<MyItem> {}
I populate a MyItems object with a list of random items. Now I need to break this list into smaller lists, whereby each list contains only the items that have the same 'Email' property.
Can this be achieved using Linq?
If you have a List<MyItem> you can simply use GroupBy:
myItems.GroupBy(x => x.Email).Select(g => g.ToList());
This will return a IEnumerable<List<MyItem>>
Or you can use ToDictionary and you can access your groups by Email:
myItems.GroupBy(x => x.Email).ToDictionary(x => x.Key, x => x.ToList());
Try this
MyItems myItems = new MyItems();
//Populate here
myItems.GroupBy(x => x.Email).Select(group => group.ToList());
Here you get a collection of lists that you can use
I have a multithreaded app that is creating a list of strings on a BlockingCollection queue, I want to take that list of strings and convert it to a collection of item objects in one or 2 steps
Is it possible to create a func<> or lamda method to achieve this type of result
public class item
{
public string name { get; set; }
public item(string nam)
{
name = nam;
}
}
IList<string> alist = new string[] { "bob","mary"};
Where you take a Ilist<> or IEnumerable<> of type string and return IList
So for the single item Func<>
Func<string, item> func1 = x => new item(x);
But essetially the signiture would look like
Func<IEnumerable<string>,IList<item>> func2 = x=> x.ForEach(i => func1(i));
Am I trying to put a round peg in sqaure hole or is my syntax/logic just wrong
Thanks in advance
Are you just trying to "reshape" the IList<string> as IList<item>?
IList<string> listOfStrings = new string[] { "bob","mary"};
IList<item> listOfItems = listOfStrings.Select(s => new item(s)).ToList();
You will have to use a Select projection instead of ForEach and then convert the resulting IEnumerable<item> to a list using ToList() - this should work:
Func<IEnumerable<string>,IList<item>> func2 = x => x.Select( i => new item(i)).ToList();
IEnumerable<item> myfunc(IEnumerable<string> stringlist)
{
var q = from s in stringlist
select new item(s);
return q.ToList();
}
I have a method as follows. It returns a list of MyTypes which appear to be ordered by myType.Id ascending by default. I'd like this list to be ordered by the ids parameter I pass into the method.
public List<MyType> GetMyTypes(List<int> ids)
{
return (from myType in db.MyTypes
where ids.Contains(myType.Id)
select new MyType
{
MyValue = myType.MyValue
}).ToList();
}
So if ids contains
302
300
301
the List returned contains items in ascending order.
What do I need to do to return List<MyType> in the order of ids?
Thanks
edit: I've tried orderby ids.IndexOf(myType.Id) but it throws the exception Method 'Int32 IndexOf(Int32)' has no supported translation to SQL.
EDIT: now that the mistake I made in understanding the requirement has been pointed out, I suggest this as a more performant method of achieving the desired result:
public static List<MyType> GetMyTypes(List<int> ids)
{
int index = 0;
Dictionary<int, int> positions = ids.ToDictionary(c => c, c => index++);
MyType[] results = new MyType[ids.Count];
foreach (MyType aType in (from myType in db.MyTypes
where ids.Contains(myType.Id)
orderby myType.Id
select myType))
{
results[positions[aType.Id]] = aType;
}
return results.ToList();
}
This won't do a search through the ids list for every element in db.MyTypes (which is a good thing: it'll be fast!).
My original (incorrect) answer:
Use an orderby clause.
public List<MyType> GetMyTypes(List<int> ids)
{
return (from myType in db.MyTypes
where ids.Contains(myType.Id)
orderby myType.Id
select new MyType
{
MyValue = myType.MyValue
}).ToList();
}
It's not clear what type of object db.MyTypes returns but, at a guess, the code could be simplified a little by avoiding the newing up of more MyType objects.
public List<MyType> GetMyTypes(List<int> ids)
{
return (from myType in db.MyTypes
where ids.Contains(myType.Id)
orderby myType.Id
select myType).ToList();
}
Daniel is nearly right, but an easy way to use the order of the incoming list is to order by the index of the ID in that list:
public List<MyType> GetMyTypes(List<int> ids)
{
return (from myType in db.MyTypes
where ids.Contains(myType.Id)
orderby ids.IndexOf(myType.Id)
select myType).ToList();
}
Note I don't know if that will work with a SQL query, so this may be Linq-to-objects only. It could also be very inefficient with large lists of ids.
The following worked:
public List<MyType> GetMyTypes(List<int> ids)
{
var unordered = (from myType in db.MyTypes
where ids.Contains(myType.Id)
select new MyType
{
MyValue = myType.MyValue
}).ToList();
var ordered = (from uo in unordered
orderby ids.IndexOf(uo.Id)
select uo).ToList();
return ordered;
}
i have a objectA
public class objectA
{
public int Id;
public string Name;
}
i have a list of objectA
List<objectA> list;
i want to find in the list any objectA with Id = 10;
is there linq syntax for this or do i simply have to write a loop here.
list.Where(o => o.Id == 10);
Remember: you can chain those method calls, or you can use the IEnumerable returned here for things like databinding.
To return all objects with an Id of ten, you'll need:
list.Where(o => o.Id = 10)