I have two list of members like this:
Before: Peter, Ken, Julia, Tom
After: Peter, Robert, Julia, Tom
As you can see, Ken is is out and Robert is in.
What I want is to detect the changes. I want a list of what has changed in both lists. How can linq help me?
Your question is not completely specified but I assume that you are looking for the differences as sets (that is, ordering does not matter). If so, you want the symmetric difference of the two sets. You can achieve this using Enumerable.Except:
before.Except(after).Union(after.Except(before));
As an alternative to linq answers, which have to pass both lists twice, use HashSet.SymmetricExceptWith():
var difference = new HashSet(before);
difference.SymmetricExceptWith(after);
Could be considerably more efficient.
Another way:
before.Union(after).Except(before.Intersect(after))
Here is the version having O(n) complexity, provided your sequences are both ordered:
public static IEnumerable<T> SymmetricDifference<T>(IEnumerable<T> coll1, IEnumerable<T> coll2, IComparer<T> cmp)
{
using (IEnumerator<T> enum1 = coll1.GetEnumerator())
using (IEnumerator<T> enum2 = coll2.GetEnumerator())
{
bool enum1valid = enum1.MoveNext();
bool enum2valid = enum2.MoveNext();
while (enum1valid && enum2valid)
{
int cmpResult = cmp.Compare(enum1.Current, enum2.Current);
if (cmpResult < 0)
{
yield return enum1.Current;
enum1valid = enum1.MoveNext();
}
else if (cmpResult > 0)
{
yield return enum2.Current;
enum2valid = enum2.MoveNext();
}
else
{
enum1valid = enum1.MoveNext();
enum2valid = enum2.MoveNext();
}
}
while (enum1valid)
{
yield return enum1.Current;
enum1valid = enum1.MoveNext();
}
while (enum2valid)
{
yield return enum2.Current;
enum2valid = enum2.MoveNext();
}
}
}
public static IEnumerable<T> SymmetricDifference<T>(IEnumerable<T> coll1, IEnumerable<T> coll2)
{
return SymmetricDifference(coll1, coll2, Comparer<T>.Default);
}
Related
for instance, let's say I want to do something like this:
bool foo(List<strings> stringList, int counter)//assume this list has, like, 10 elements, and counter=3, idk
{
bool found= false;
for(int i=0; i<stringlist.Count && !found; i++)
{
if(stringlist[i].length < 2 || counter >=6)
found=true;
counter++;
}
return found
}
Now, Is that equivelent to this:
bool foo(List<strings> stringList, int counter)//assume this list has, like, 10 elements, and counter=3, idk
{
bool found= false;
foreach(string s in stringlist.Takewhile(x=> (!found)))
{
if(s.length < 2 || counter >=6)
found=true;
counter++;
}
return found
}
Does this second example behave like the first, or does it always skip the whole loop? As a follow up, if I still want to use a foreach, do I really have to use a break to get around this? Also, sorry if I did something dumb in these examples, I am trying to simplify a version of a path-finding algo I am writing and this was the simplest example I could think of to ask this question...
As the documentation of Microsoft here :
The TakeWhile(IEnumerable, Func)
method tests each element of source by using predicate and yields the
element if the result is true. Enumeration stops when the predicate
function returns false for an element or when source contains no more
elements.
and this is the decompiled TakeWhile() method :
public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null)
throw Error.ArgumentNull(nameof (source));
if (predicate == null)
throw Error.ArgumentNull(nameof (predicate));
return Enumerable.TakeWhileIterator<TSource>(source, predicate);
}
private static IEnumerable<TSource> TakeWhileIterator<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
foreach (TSource source1 in source)
{
if (predicate(source1))
yield return source1;
else
break;
}
}
as you can see, yes the TakeWhile() method loops through all the elements doing the check and it's internal loop is irrelevant to your outer loop.
Is there a way to implement an extension method to a generic type that takes in arguments a Func of another type?
For exemple, a usage something similar to this:
myFirstObject.Extension<myOtherObject>( other => other.Prop );
Or with a more complicated Func:
myFirstObject.Extension<myOtherObject>( other => other.Prop > 2 && other.Prop < 15 );
I found some related question like this one, but in my case, I need generic types inside the extension method too.
Here's what I came up with:
public static bool Extension<TSource, TIn, TKey>(this TSource p_Value, Expression<Func<TIn, TKey>> p_OutExpression)
{ return true; }
However, when I try to use it, it does not take into consideration the second type.
Am I missing something?
Look at this:
s => s.Length;
How's the compiler suppose to know whether or not s is a string or s is an array or some other type that has a Length property? It can't, unless you give it some information:
(string s) => s.Length;
Oh, there we go. So now, try this:
myFirstObject.Extension((myOtherObject o) => o.Prop > 2 && o.Prop < 15);
That will work, because you've told the compiler what it should use for TIn, and it can figure out what to use for TKey based on the expression.
I found that another solution would be to create another method that takes in argument a type.
For instance:
Void Extension(Type p_Type, [THE TYPE] p_Params)
{
MethodInfo realExtensionMethod = typeof([CLASS CONTAINING THE METHOD]).GetMethod("RealExtension");
realExtensionMethod = realExtensionMethod.MakeGenericMethod(p_Type);
realExtensionMethod.Invoke(null, new object[] {p_Type, p_Params });
}
Void RealExtension<TYPE>(params)
{
}
Then at usage time:
Type objectType = typeof(myOtherObject);
myFirstObject.Extension(objectType, other => other.Prop );
When you call a generic method in C# you can explicitly declare all of the generic type parameters or you can have them all inferred, but you cannot have some explicitly declared and some inferred.
So, if I had this method:
public void Foo<X, Y>(X x, Y y)
{
/* Do somethhing */
}
Then here's what works and what doesn't:
int a = 42;
string b = "Hello, World!";
// Legal
Foo(a, b);
Foo<int, string>(a, b);
//Illegal
Foo<int>(a, b);
The best you can do is move the first generic parameter up to the class level, but not it won't work as an extension method. Nevertheless you may like this approach.
public static class Class<TSource>
{
public static bool Method<TIn, TKey>(
TSource p_Value,
Expression<Func<TIn, TKey>> p_OutExpression)
{
return true;
}
}
Now you can call it like this:
Expression<Func<long, decimal>> f =
l => (decimal)l;
var result = Class<int>.Method(a, f);
But as I say, it won't work as an extension method now.
I've been following the Tasky Case Study on the MonoDroid website, but I've run into a wall whilst creating a filtering and ordering query. I was wondering if somebody could explain where I might be going wrong please? I'm probably doing something completely backwards!
I get the error:
The type arguments for method 'ICanTalk.BusinessLayer.Services.WordServices.Find(System.Func, System.Func, bool, int, int)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
I have the following code in one of my repositories, hopefully what I'm trying to do is kind of clear. I haven't been able to build it yet to test to see if it works though:
public static IEnumerable<Word> Find<T,U>(Func<Word, bool> whereClause, Func<Word,U> orderBy, bool ascending, int show, int page)
{
int currentPage = page;
int resultsPerPage = show;
int skip = currentPage*show;
var result = ascending
? me.db.Find<Word>().Where(whereClause).OrderBy(orderBy).Take(resultsPerPage).Skip(skip)
: me.db.Find<Word>().Where(whereClause).OrderByDescending(orderBy).Take(resultsPerPage).Skip(skip);
return result;
}
From my services I call this method from here:
public static IList<Word> Find<T>(Func<Word, bool> whereClause, Func<Word,DateTime> orderBy, bool ascending, int show, int page)
{
return WordRepository.Find<Word, DateTime>(whereClause, orderBy, ascending, show, page).ToList();
}
What I'm trying to achieve is a call from an event handler within MonoDroid like:
var wordTest = WordServices.Find(x => x.ChildId == 3, x => x.AddedAt, true, 5, 1);
Both of your methods are generic Find<T,U> and Find<T> - but you don't seem to be using the type T anywhere in your method definition. Was T expected to be the type Word ?
U is used once in the orderBy where elsewhere you're using bool - which of those usages is a mistake?
Can you try replacing Word with T in your definitions, and bool with U (if that's what you meant to do) and then when you're calling the method actually call it with the correct type ?
var wordTest = WordServices.Find<Word>(x => x.ChildId == 3,
x => x.AddedAt,
true, 5, 1);
I had it almost right, I stripped it down to the basics and worked my way back up and came up with the following (compare to original if you're interested).
SQLiteDatabase.cs
(not in original post - I scaled the query clauses right down to a reusable method in my generic handler):
public IEnumerable<T> Find<T>(Func<T, bool> whereClause, int resultsToSkip, int resultsToShow)
where T : BusinessLayer.Contracts.IBusinessEntity, new()
{
lock (locker)
{
return Table<T>().Where(whereClause).Skip(resultsToSkip).Take(resultsToShow).ToList();
}
}
WordRepository.cs
public static IEnumerable<Word> Find<T>(Func<Word, bool> whereClause, int resultsToSkip, int resultsToShow)
where T : BusinessLayer.Contracts.IBusinessEntity, new()
{
var result = me.db.Find<Word>(whereClause, resultsToSkip, resultsToShow);
return result;
}
WordServices.cs
By this point I'm wondering why there exists a need for so many layers - will definitley look to refactor in the morning.
public static IList<Word> Find<T, U>(Func<Word, bool> whereClause, Func<Word, U> orderBy, bool ascending, int show, int page)
{
int resultsToShow = show;
int resultsToSkip = show * (page - 1);
var result = ascending ? WordRepository.Find<Word>(whereClause, resultsToSkip, resultsToShow).OrderBy(orderBy)
: WordRepository.Find<Word>(whereClause, resultsToSkip, resultsToShow).OrderByDescending(orderBy);
return result.ToList();
}
Originating Call
var wordTest1 = WordServices.Find<Word, DateTime>(x => x.ChildId == 1, x => x.AddedAt, true, 5, 1);
How do you do Linq with non-lambda express for the following (which does not work):
string[] words = { "believe", "relief", "receipt", "field" };
var wd = (from word in words
select word).Any(Contains ("believe"));
It's not clear what good you believe the from wor in words select wor is doing - it's really not helping you at all.
It's also not clear why you don't want to use a lambda expression. The obvious approach is:
bool hasBelieve = words.Any(x => x.Contains("believe"));
Note that this isn't checking whether the list of words has the word "believe" in - it's checking whether the list of words has any word containing "believe". So "believer" would be fine. If you just want to check whether the list contains believe you can just use:
bool hasBelieve = words.Contains("believe");
EDIT: If you really want to do it without a lambda expression, you'll need to basically fake the work that the lambda expression (or anonymous method) does for you:
public class ContainsPredicate
{
private readonly string target;
public ContainsPredicate(string target)
{
this.target = target;
}
public bool Apply(string input)
{
return input.Contains(target);
}
}
Then you can use:
Func<string, bool> predicate = new ContainsPredicate("believe");
bool hasBelieve = words.Any(predicate);
Obviously you really don't want to do that though...
EDIT: Of course you could use:
var allBelieve = from word in words
where word.Contains("believe")
select word;
bool hasBelieve = allBelieve.Any();
But that's pretty ugly too - I'd definitely use the lambda expression.
You could do it like that
string[] words = { "believe", "relief", "receipt", "field" };
var wd = (from wor in words
where wor.Contains("believe")
select wor);
I know this has been an issue for a while now, and checked all previously answers I could get, but still this one doesn't work.
The object 'crew' represents crewmembers with ranks and other items. The comparison should be made by comparing 'assigned_rank', an int value, and if this value is equal in both instances, then 'is_trainer', a boolean, should make the difference.
This method worked great as long as it was running with java < 7. But since Java 7 I keep getting this one:
java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeLo(ComparableTimSort.java:714)
at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:451)
at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:376)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:182)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at java.util.Collections.sort(Collections.java:155)
at dormas_flightlog.Query.getCrew(Query.java:714)
Here is the source, where some potentially dangerous parts have allready been out-commented, but it still does not work:
public class crew implements Serializable, Comparable<crew> {
private static final long serialVersionUID = 36L;
private int flightID = 0;
private int assigned_rank = 25;
private boolean is_trainer = false;
...
#Override
public int compareTo(crew him) {
int myRank = this.getAssigned_rank();
int hisRank = him.assigned_rank;
if (this == him) {
return 0;
}
if (myRank > hisRank) {
return 1;
}
if (myRank < hisRank) {
return -1;
}
if (myRank == hisRank) {
// if (is_trainer && !o.is_trainer) {
// i = 1;
// }
// if (!is_trainer && o.is_trainer) {
// i = -1;
// }
// if (is_trainer && o.is_trainer) {
// i = 0;
// }
// if (!is_trainer && !o.is_trainer) {
// i = 0;
// }
return 0;
}
return 0;
}
#Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + this.assigned_rank;
hash = 31 * hash + (this.is_trainer ? 1 : 0);
return hash;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
int myRank = this.getAssigned_rank();
int hisRank = 0;
if (o instanceof crew) {
crew him = (crew) o;
hisRank = him.assigned_rank;
} else {
return false;
}
if (myRank > hisRank) {
return false;
}
if (myRank < hisRank) {
return false;
}
if (myRank == hisRank) {
// if (is_trainer && !o.is_trainer) {
// i = 1;
// }
// if (!is_trainer && o.is_trainer) {
// i = -1;
// }
// if (is_trainer && o.is_trainer) {
// i = 0;
// }
// if (!is_trainer && !o.is_trainer) {
// i = 0;
// }
return true;
}
return false;
}
}
Implementing equals() was just a try to solve this problem. The given exception comes with or without equals(). I cannot see how the compareTo-method violates its contract. Any help is greatly appreciated....one day this code has to work with java 7 and I don't know how...
Thanks
see this:
From http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#source
Area: API: Utilities Synopsis: Updated sort behavior for Arrays and
Collections may throw an IllegalArgumentException
Description: The sorting algorithm used by java.util.Arrays.sort and
(indirectly) by java.util.Collections.sort has been replaced. The new
sort implementation may throw an IllegalArgumentException if it detects
a Comparable that violates the Comparable contract. The previous
implementation silently ignored such a situation. If the previous
behavior is desired, you can use the new system
property java.util.Arrays.useLegacyMergeSort, to restore previous
mergesort behavior.
Nature of Incompatibility: behavioral
RFE: 6804124
For more detailed info, see the bug database reference here.
maybe you just have NaN values which you compare through Collections.sort(...), this has been a problem to me and I got that exception even having right implementation of compare(obj1, obj2) method! Check that!
I was able to solve this error cause it was a bug in jdk7.
here I found the solution:
"Comparison method violates its general contract!" - TimSort and GridLayout
Basically i just had to add the
JAVA_OPTS="$JAVA_OPTS -Djava.util.Arrays.useLegacyMergeSort=true"
to my jboss
Unfortunately, none of the solutions work for Android. TimSort is used deep in Android's ViewGroup relating to addChildrenForAccessibility that shows up under Java 7 & 8. No user code is involved in any comparison.
From other reports, it is related to having RelativeLayout with overlapping items as is commonly done. For example, a TextView that appears over an Image, or two items at the same location, where you only set one visible at a time.
https://code.google.com/p/android/issues/detail?id=55933
I've not found any way around the bug. You can't set a -Djava option in Android Studio or Eclipse (at least that I could find). Forcing use of Java 1.6 should work, but doesn't. Seems like Amazon's newer Fire tablets and phones are far more sensitive to this bug than other devices.
There are rumors Java 9 will have a fix such as a run-time option that works, but with a bug that's been around for years, I have doubts it will ever be fixed - especially considering the animosity between Oracle and Google. Any yes, perhaps the bug is really deep in the Android code and should be fixed there. With more than a billion devices out there, that's not a viable solution for all the existing devices.