c#: Lambda expression is not a delegate type
I was changing some code from Silverlight to WPF and ran into this error:
Cannot convert lambda expression to type 'System.Windows.Threading.DispatcherPriority' because it is not a delegate type
The code was something like this
gui.BeginInvoke(()=>{Orders = new ObservableCollection<Order>(x);},null);
The problem is described in detail here:
The article suggests a couple of ways around this, but the best solution in my opinion is found in the comments:
Cast the lambda to an Action explicitly.
It's fast to do, easy and intuitive:
gui.BeginInvoke((Action)(()=>{Orders = new ObservableCollection<Order>(x);}),null);
This compiles and runs fine.
Happy holidays
/Henri
c#: switch multiple values in case 1,2,3 using extension methods in(1,2,3)
I want to check if a variable contains one of several values, and if so do something about it.
Here's the usual switch way:
switch (value)
{
case 1:
case 2:
case 3:
//do some stuff
break;
case 4:
case 5:
case 6:
//do some different stuff
break;
default:
//default stuff
break;
}
Does that feel ok to you?
To me it's just feels too verbose and cludgy.
In VB it's possible to write like this:
Select Case value
Case 1 To 3
'do some stuff
Case 4, 5, 6
'do some different stuff
Case Else
'default stuff
End Select
which is way more elegant.
So how do we get this in c#?
I ended up with this solution:
if(value.In(1,2,3))
{ /* do some stuff */ }
else if (value.In(4,5,6))
{ /* do some different stuff */ }
else
{ /*default stuff */ }
You likey - likey? ;)
And it's also more flexible than a switch statement.
The magic behind is just this sweet little extension method:
public static class Extensions
{
public static bool In<T>(this T t, params T[] values)
{ return values.Contains(t); }
}
Me likey
Henri
TFS: reattach solution to TFS, and compare folder to source control
I recently was in a situation where I had to add and change files in a solution that was originally in TFS,
but where TFS was unavailable because of migration to new hardware.
So I removed source control bindings and programmed away.
Now the server was up again, and I wanted to add my changes back to TFS.
I opened the solution in VS 2010, right-clicked it in Solution explorer and selected Add to source-control.
That gave me a warning and asked me to use the command Change source control, as it found this solution already in TFS (the "old" version).
So I went to File - source control - Change source control, and there I could bind my solution back to TFS by selecting the projects and clicking Bind.
But this only gets my already known solution files bound to TFS.
All new files added in the meantime is not detected.
All modified files are not marked as checked out.
So I could of course try to remember what files I changed, added etc., or even get the latest version from TFS into another folder, and then do a compare from there.
But as it turns out, TFS has this capability built right in to Visual Studio, if you only know where to look.
Open up Soure Control Explorer (View - Other Windows - Source Control Explorer)
Find your solution in the tree and select it. Then click this button:
The next window will give you alternatives regarding what changes you want to find, defaults are fine with me:
Select ok, and voila!
You now get a window showing you all the files that are different, new etc, and the options to do something about it.
Great stuff, and easier than I had imagined when I started looking for it.
regards
Henri
c#: Linq/Lamba remove duplicates from list
I have a list as a result of a join, and it contains duplicates.
private IEnumerable<Sak> LagListeMedSaker(IEnumerable<KorrespondanseInfo> korr, IEnumerable<Saksinfo> liste)
{
var result = from l in liste
join k in korr
on l.Saksnummer equals k.Saksnummer
select new Sak
{
Saksnr = l.Saksnummer,
Tag = l.Tag
};
return result;
}
The list korr can contain duplicates of the same Saksnummer, even though the korr liste entries are unique in it self, i.e they contain other properties that are different.
Now I want to remove duplicates of the result list.
I wanted to use .Distinct something like this:
//remove duplicates, not working (nor compiling..)
result= result.Distinct(t=>t.Saksnr);
but Distinct doesn't support Funcs, and rather requires you to create an implentation of IEqualityComparer.
Btw the default .Distinct() without parameters won't work correctly for your custom objects unless you provide correct
implementations og Equals and GetHashCode. See http://blogs.msdn.com/b/csharpfaq/archive/2009/03/25/how-to-use-linq-methods-to-compare-objects-of-custom-types.aspx
for more info if interested. So I'm feeling lazy, and want to do this in just a line or two, what are the options?
I went with this solution someone posted at stackoverflow (lost the link sorry):
result= result.GroupBy(sak => sak.Saksnr).Select(y => y.First());
I really like this as it is really fast to implement and change according to your needs.
You can also group on multiple values by using an anonymous type, i.e:
result = result.GroupBy(sak => new { sak.Saksnr, sak.Tag }).Select(y => y.First());
See for example http://www.devcurry.com/2009/02/groupby-multiple-values-in-linq.html for full example of grouping on multiple values.
PS. I found another post using this kind of solution on stackoverflow, this contains both these concepts:
(see post from David B)
PSPS. One could use HashSet<T> to get unique entries but that requires mostly the same as when you use .Distinct(), i.e either provide IEqualityComparer or override GetHashCode and Equals.
PSPSPS. Yes, I should have removed the duplicates before the join, …sorry :)
Subscribe to:
Posts
(
Atom
)


No comments :
Post a Comment