score:1
i believe the following should do the trick:
contract.requires(
contract.forall(
coll,
obj => (coll.where(x=>x.propa = obj.propa).count==1)
)
);
the theory is that it filters coll to only those elements whose value of propa is the same as the object we are looking at. there should only be one of these (itself).
you can repeat similarly for b.
and it is in theory trivial to combine the forall lambda expressions but i'm not sure you'd want to. surely it would be nice to know which condition fails if one does rather than lumping them all together and knowing that something failed but not really what...
if you can give a bit of leeway on the format you can try:
contract.requires(
contract.forall(
coll.groupby(x=>x.propa),
group => group.count==1)
)
);
this is a similar principle but i think will do the counting more efficiently since the group by and count will be more efficient (i think - i've not tested and am not famiilar with the inner workings of the linq methods).
another method:
hashset<object> propavalues = new hashset<object>();
contract.requires(
!coll.any(x=>!hashset.add(x.propa))
);
this uses a hashset and the fact that add returns false if the element already exists. in this case the moment an add generates a false (and thus the lambda expression is true) then any will return true which since it is negated will fail the test.
whether this method is sensible or not probably depends on how big your objects are (and thus the potential memory implications of doubling up your object set. it will however take least iterations to terminate compared to the other methods here (since the other methods need to look at every object in the collection, potentially several times whereas this last one could potentially stop after looking at two entries).
score:2
i might be completely off base here, never having used contracts, but assuming contract.requires
can be passed an arbitrary bool
, can't you just do:
contract.requires(coll.groupby(o => o.propa).count() == coll.count);
and similarly for propb
?
Source: stackoverflow.com
Related Query
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- How to find keys in a Dict<int,List<Tuple<string,string>>> such that the list contains elements with given Item1 and Items
- linq how to select a parent with a child collection that contains all of an array (or list) of values
- Linq - How to select items from a list that contains only items of another list?
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- linq - how do you do a query for items in one query source that are not in another one?
- linq how to select the parent from a collection where the parent contains child items in another collection
- C#: How to remove items from the collection of a IDictionary<E, ICollection<T>> with LINQ?
- How can I select using LINQ for an entry that contains a LIST with more than one row?
- How to order a collection with LINQ so that a particular string appears first
- How to check if items in collection contains a specific value?
- How to match two Lists with only items that are different in Linq
- How can I ensure that my database connection will be closed with the deferred execution of a linq query?
- WPF: How do I fill a datagrid with NWS XML Data that contains Attributes with spaces using LINQ
- How to select items that do not show up in a second list with Linq
- How to match string in 2 diff collection and return the collection items that did not match in LINQ
- How do I return all items in a collection that possess an interface property of a specific type?
- How to efficiently filter a list in c# with linq that contains certain attributes
- How to query with Linq a List that contains a list of lists that contains a list of lists that contains a list of objects?
- How to order by 2 fields with relation child/parent. Sort it so that child items follow their parents in the sorted list?
- How can I get a string that contains all the selected items in a multiselectlist?
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
- How to merge a list of lists with same type of items to a single list of items?
- Error: "The specified LINQ expression contains references to queries that are associated with different contexts"
- How are people unit testing code that uses Linq to SQL
- What does this C# code with an "arrow" mean and how is it called?
- How to count the number of elements that match a condition with LINQ
- How to determine if XElement.Elements() contains a node with a specific name?
- Using LINQ to Objects to find items in one collection that do not match another
More Query from same tag
- Can't use LINQ on CookieCollection despite the enumerator being accessible
- join two tables in linq with special conditions
- Group By 'MM-dd-yyyy hh-mm'
- Linq Detect Non Zero GUIDs in an array?
- How to convert list of arrays into a multidimensional array
- LINQ only select data after current time for today
- How to get rows of Data from Multiple Tables using LinQ To Entities
- Filter a generic list based on another list
- LINQ on IList of objects
- Entity Framework, why is this sql being generated?
- How to create a class for a MongoDB collection that is not mine?
- Appending results from LINQS query to paged list
- .NET and Hadoop - What should I know / learn and what is available?
- filter IQueryable in a loop with multiple Where statements
- Need to compare 2 generic list of same type based on dynamic columns/fields that will be know at run time
- FormViewModel in MVC for Relations
- how to convert UTC dateTime to local datetime
- How to format date in LINQ without explicity listing every column?
- LINQ get the data from the datatable group by hours
- use Linq with access database
- Linq receiving unable to create value type of model, only primitive types or enumeration types are supported
- Why is mongo return zero matching document although there is one?
- Group by clause in linq ? Joining table in linq?
- C# LINQ GroupJoin skip item
- Get keys from IList<IDictionary<string, object>>
- Using LINQ/Delegates, how to initialize a property belong to all the members of an Object of a type list?
- Skip first row in a Matrix, and validate width/height
- Linq to Entities: Next Value in Sequence
- LINQ Aggregate special attention for last element of list
- Get Top 10 most sold products between date