There are two lists "ObjlistA" and "ObjlistB".
var newList = from someObg i ObglistB
where [condition = true if some property of any element in the list ObjlistAA equals someObg's some property]
select someObg
Is there a way to loop inside the where clause, so that the obj's property can be compared with each and every element's property in the list?
Can anyone help me out the "Where" part?
Is this what you're looking for?
var newList = ObjlistB.Where(someObj => ObjlistA.Any(a => a.SomeProperty == someObj.SomeProperty))
where ObjlistA.Any(x => x.Property == someObj.Property)
you mean this?
var newList = from someObg in ObjlistB
where ObjlistA.Any(a => a.ID == someObg.ID)
select someObg;
Related
How to write this query? :
var parents = parents.Select(o => o.children.Where(p=>p.property1 == "Something")).ToList();
This comes up with the conversion type error. How can I return some parents based on a condition being true for their children's properties?
This is what your query could be:
parents = parents.Where(p => p.children.Any(c => c.property1 == "Something")).ToList();
Enumerable.Where filters a sequence of values based on a predicate whereas Enumerable.Select projects each element of a sequence into a new form.
Enumerable.Any would return true if there be at least 1 child with porperty1 equal to "something"
As all you need to do here is the filtering, you just need to use Where. You would have used Select if you wanted to create a collection of some type other than that of the parent itself.
Try Code
var parents= (from p in parents.AsEnumerable()
where p.children!=null && p.children.property1 =="Something"
select p).ToList()
I have a List within a list. I only need the one value from the list and can do obtain my result with a nested foreach but I want to use a LINQ query of some sort.
my code:
var myCity = from c in CountryLists
select (from city in c.stateList
where city.name == passedInValue
select city.name).FirstorDefault();
This returns myCity as a list of some sort with all values as null EXCEPT for where the match was found.
i don't want to have to walk through the city list to find the name. How can I have only one value in myCity; either null or the desired name?
First, use SelectMany to flatten the list, then FirstOrDefault to filter:
CountryList.SelectMany(c => c.stateList).FirstOrDefault(d => d.Name == passedInValue);
Note that because FirstOrDefault can take a predicate, you don't actually need the Where clause.
How about using SelectMany:
var city = CountryLists
.SelectMany(x => x.stateList)
.FirstOrDefault(x => x.name == passedInValue);
You can use SelectMany as other have pointed out (and I prefer that solution myself), however if you'd like the query syntax, you can use multiple from clauses (check the MSDN documentation for more examples):
var city = (from c in CountryLists
from city in c.stateList
where city.name == passedInValue
select city.name).FirstOrDefault();
It is equivalent to the SelectMany method solution, it uses it under the covers anyway.
I have a generic List List[int, myClass], and I would like to find the smallest int value, and retrieve the items from the list that match this.
I am generating this from another LINQ statement
var traysWithExtraAisles = (from t in poolTrays
where t.TrayItems.Select(i=>i.Aisle)
.Any(a=> ! selectedAisles.Contains(a))
select new
{
count= t.TrayItems.Select(i=>i.Aisle)
.Count(a=> !selectedAisles.Contains(a)),
tray=t
}).ToList();
this gives me my anonymous List of [count, Tray], but now I want to figure out the smallest count, and return a sublist for all the counts that match this.
Can anyone help me out with this?
var smallestGroup = traysWithExtraAisles
.GroupBy(x => x.count)
.OrderBy(g => g.Key)
.First();
foreach(var x in smallestGroup)
{
var poolTray = x.tray;
}
You can use SelectMany to "flatten" your list. Meaning, combine all of the lists into one, then take the Min. So;
int minimum = poolTrays.SelectMany(x => x).Min(x => x.TheIntegerIWantMinOf);
Will give you the smallest value contained in the sub lists. I'm not entirely sure this is what you're asking for but if your goal is simply to find the smallest element in the collection then I would scrap the code you posted and use this instead.
Right, I now realise this is actually incredibly easy to do with a bit more fiddling around. I have gone with
int minCount = traysWithExtraAisles.Min(x=>x.count);
var minAislesList = (from t in trayswithExtraAisles
where t.count==mincount
select t).ToList()
I imagine it is probably possible to do this in one statement
You can use GroupBy as answered by Tim... or OrderBy as follow:
var result = traysWithExtraAisles.OrderBy(x=>x.count)
.TakeWhile((x,i)=> i == 0 || x.count == traysWithExtraAisles[i-1]).count;
At the moment I have to break down this simple operation in two parts, I am sure the would be a better way is hiding from me :
List<int> selectedValues= new List<int>();
...
IEnumerable<RadComboBoxItem> checkedItems = from checkedItem in cblMagistrateCourts.Items.ToList()
where checkedItem.Checked == true
select checkedItem;
foreach (RadComboBoxItem item in checkedItems)
{
if (item.Checked)
selectedValues.Add(Convert.ToInt32(item.Value));
}
I am wanting this to be done server-side only.
How about this?
List<int> selectedValues = cblMagistrateCourts.Items.Where(i => i.Checked)
.Select(i => Convert.ToInt32(i.Value))
.ToList();
Convert the value at same time you're selecting the checkedItem...
List<int> selectedValues = (from checkedItem in cblMagistrateCourts.Items.ToList()
where checkedItem.Checked == true
select Convert.ToInt32(checkedItem.Value)).ToList();
rcb.CheckedItems.Select(x => x.Value).ToList();
How do you loop through IQueryable and remove some elements I don't need.
I am looking for something like this
var items = MyDataContext.Items.Where(x => x.Container.ID == myContainerId);
foreach(Item item in items)
{
if(IsNotWhatINeed(item))
items.Remove(item);
}
Is it possible? Thanks in advance
You should be able to query that further as in this
var filtered = items.Where(itm => IsWhatINeed(itm));
Also notice the subtle change in the boolean function to an affirmative rather than a negative. That (the negative) is what the not operator is for.
items = items.Where( x => !IsNotWhatINeed(x) );
var items = MyDataContext.Items.Where(x => x.Container.ID == myContainerId
&& !IsNotWhatINeed(x));
or
var items = MyDataContext.Items.Where(x => x.Container.ID == myContainerId)
.Where(x=> !IsNotWhatINeed(x));
The other answers are correct in that you can further refine the query with a 'where' statement. However, I'm assuming your query is a Linq2Sql query. So you need to make sure you have the data in memory before further filtering with a custom function:
var items = MyDataContext.Items.Where(x => x.Container.ID == myContainerId)
.ToList(); // fetch the data in memory
var itemsToRemove = items.Where(IsNotWhatINeed);
If you really want to extend the IQueryable, then the 'IsNotWhatINeed' function must be translated to something that Linq2Sql understands.
Try This:
var items = YourDataContext.Items.Where(x => x.Container.ID == myContainerId
&& !IsNotWhatYouNeed(x));