I am trying to figure out why my delegate function does not work, any help would be appreciated, this is probably a small issue but I have been working on it for a while and connot figure it out, my code:
//remove all matching people from this list
public void RemovePeopleFromLookup(Predicate<PeopleDTO> _people)
{
//Lookup is an internal readonly ICollection of PeopleDTO
Lookup.RemoveAll(_people);
}
//call the method as below:
//data is a collection of PeopleDTO
mylookupobj.RemovePeopleFromLookup(x => data.Any(y => y.Name == x.Name && x.Type == FieldElement.Strange));
For some reason all people get removed from this lookup, this is not correct, I only want to remove the people who are
Strange
who DONOT exist in the data collection
EDIT:
The data collection can be an object of different types -> strange, noisy etc...
The mylookupobj.Lookup data collection is similar to the data collection and contain contain multiple types hence why I wrote my query that way
EDIT2: I missed out this information which may be very important...
public class PersonDTO
{
//Name
//Type
//Age
//Desc
}
Inside the mylookupobj.Lookup - all the properties contain data, however inside the data collection only the Name + Type is present.
A simpler and more efficient predicate would be:
x => (x.Type == FieldElement.Strange) && data.Any(y => y.Name == x.Name)
But I admit I don't see a principal problem with either.
Edit: seems 1 of the conditions has to be inverted.
x => (x.Type == FieldElement.Strange) && ! data.Any(y => y.Name == x.Name)
The call to Any is the problem. Essentially it's running through the collection multiple times. i.e. if any of the objects in the collection matches the condition, remove the item. Try this:
mylookupobj.RemoveFieldFromLookup(y => y.Name == x.Name && x.Type == FieldElement.Strange);
Please try this.
mylookupobj.RemovePeopleFromLookup(x => data.Contains(y => y.Name == x.Name)
&& x.Type == FieldElement.Strange);
Related
I have a list of type customer. I need to insert all values of the list in the database before checking if a customer with the same customer number exists for that particular client.
For that I am firing a query to get me all customers who are there in the database having customer number equal to ones in the list. The query I am writing is not working, here's the code.
CustomerRepository.Find(x => x.ClientId == clientId)
.Where(x => x.CustomerNumber.Contains(lstCustomersInserted.Select(c => c.CustomerNumber)));
Keep it simple:
var lstCustomerNumbers = lstCustomersInserted.Select(c => c.CustomerNumber);
var res = CustomerRepository.Where(x => x.ClientId == clientId && lstCustomerNumbers.Any(c => c == x.CustomerNumber));
I think you have it backwards. Try reversing the Contains.
Edit: I switched to using the generic predicate Exists instead of Contains based on the comment, so you can match a property.
CustomerRepository.Find(x => x.ClientId == clientId)
.Where(x => lstCustomersInserted.Exists(c => x.CustomerNumber == c.CustomerNumber));
How about an Except?
CustomerRepository.Select(x => x.ClientID)
.Except(lstCustomersInserted.Select(x => x.CustomerID));
This will return the IDs of the objects in the repo that don't exist in your lstCustomersInserted.
I am fairly new to Linq and I am starting to try and filter lists and IEnumerable content without using foreach loops. In this case I want to retreive a nested property if it meets a given criteria.
Here is what I have so far:
IEnumerable<GroupTourData> test =
web_service_item.TourInstances
.Where(x => x.Availability.Where(a => a.Price > 0 && a.Grade == string.Empty)
.Count() > 0);
What I want to achieve here is an IEnumerable list of all of the prices in the matching criteria. I though this would be obtainable by adding .SelectMany(a.Price) to the end of this statement but that seems to error out in Visual Studio.
Any help or pointers would be greatly appreciated.
ok seems as this solves the issue:
if you just want the prices? then you just had your SelectMany in the wrong position:
web_service_item
.TourInstances
.SelectMany(ti => ti.Availability)
.Where(a => a.Price > 0 && a.Grade == string.Empty)
.Select(a => a.Price);
Depending on your data schema and tables... Have you tried including your sub-entities using Include?
IEnumerable<GroupTourData> test = web_service_item
.TourInstances
.Include("Availability")
.Where(x => x
.Availability
.Any(a => a.Price > 0 && a.Grade == string.Empty)
);
I want to do something like this:
var list = db.Respaldoes.Where(x => x.type == "Backup");
if (condicion1)
list.Where(x => x.entity == "GIELCJLT03");
if (condicion2)
list.Where(x => x.activity_type == "0");
I don't know if something like this is possible, I get the list but the filters are never applied.
You could try something like this:
var list = db.Respaldoes.Where(x => x.type == "Backup");
if (condicion1)
list = list.Where(x => x.entity == "GIELCJLT03");
if (condicion2)
list = list.Where(x => x.activity_type == "0");
Initially the list when the Where clause, x => x.type == "Backup", will be executed it will give you the initial list you refer to. Then, if the condicion1 is true, you will make a second fitering and you will assign the result to list. There again you have deffered execution. Only when list will be requested to be consumed by another piece of your code will be executed -hence the name deffered execution. The same holds for the second condicion and so on.
Where returns an IEnumerable<T>, which defers the execution until a later action forces the query to be resolved. If you want to resolve your query, immediately execute it with ToList().
In addition, you are not actually doing anything with your filtered list. By reassigning the filtered list to your original list object, it will update the collection with the filter, removing objects that do not match your predicate.
var list = db.Respaldoes.Where(x => x.type == "Backup");
if (condicion1)
list = list.Where(x => x.entity == "GIELCJLT03").ToList();
if (condicion2)
list = list.Where(x => x.activity_type == "0").ToList();
I have code like this:
.Where(o => o.Parents.Contains(name))
The above code does not work because Parents contain a list of Parent objects, which in turn have a property Name. I want to check against the Name property, but it's a list so how could I do this check? So I want the Where to be true when any of the Parent objects in the list have Name property set to name.
Try the following code snippet:
.Where(o => o.Parents.Any(p => p.Name == name))
There's a simple fix for this: use more LINQ.
.Where(o => o.Parents.Any(p => p.Name == name))
As an alternative, you could use the slightly more verbose (but equally lazy)
.Where(o => o.Parents.Select(p => p.Name).Contains(name))
You can use .Any to check for a specific condition inside a collection of objects where you want to check for a specific property.
.Where(o => o.Childs.Any(child => child.Name == o.Name));
I am using nHibernate to retrieve a collection of orders (and its order lines) from a Sql Server database.
This is my ShipmentOrder class:
public class ShipmentOrder
{
private ICollection<ShipmentDetail> _ShipmentsDetails;
public virtual ReadOnlyCollection<ShipmentDetail> ShipmentsDetails
{
get { return (new List<ShipmentDetail>(_ShipmentsDetails).AsReadOnly()); }
}
}
nHibernate returns a IList with all the details (ShipmentsDetails) loaded (since I Eager load them).
Now, I would like to filter my collection of ShipmentOrder and ShipmentDetail and get back a collection of ShipmentDetail.
I've tried something like this:
IList<ShipmentOrder> Results;
// Fetch orders using nHibernate
Results = FetchOrders();
var shipmentLines = Results
.Where(x => x.Company == "XXX" && x.OrderNumber == "111")
.SelectMany(x => x.ShipmentsDetails)
.Where(s => s.RowNumber == 1 && s.RowSeq == 0)
.ToList();
But I've realized that I obtain multiple results of the same line.
I've converted the lamda expression like this:
var shipmentLines = Results
.Where(x => x.Company == "XXX" && x.OrderNumber == "111")
.SelectMany(x => x.ShipmentsDetails)
.Where(s => s.RowNumber == 1 && s.RowSeq == 0)
.Distinct()
.ToList();
and it works fine.
I was wondering if there's a better way to achieve the same result without the distinct.
UPDATE:
I am using SelectMany here cause this is the only way I've found to apply filters to children (ShipmentsDetails).
this looks like Linq2Objects, therefor the IList Results contains duplicate records, probably you do eager fetching to levels deep which unfortunatly results in duplicate root entities. use distinctrootentity-resulttransformer in the query