Geeks With Blogs
Welcome to
Geeks with Blogs
Login
Michael Freidgeim
Personal Website / Bio
587 Posts
| 965 Comments
My Other Recent Posts
Batch file that required administrative privileges.
PowerShell function to delete if exist and create empty folder
Pocket on IPad: Ability to rename articles is still missing
Read CSV files with multiple lines
Tiny MCE editor in ASP.Net Web Form
Methods to verify, are DataTables or DataSets the same.
2 options to write tests for WCF Services
Angular JS vs Knockout JS -quotes and links
POSTSHARP error PS0052: The plug-in "PostSharp.Patterns.Diagnostics.Weaver" was not found
Misleading compiler error "is a 'type' but is used like a 'variable'"
News
My Blog has been MOVED to
https://mfreidge.wordpress.com
Post Categories
A Look Back In Time
Developing Agile Software
Random Musings
Contrary Views
Developers Notes
Archives
April 2016 (1)
February 2016 (9)
January 2016 (5)
August 2013 (6)
July 2013 (1)
June 2013 (5)
May 2013 (3)
March 2013 (3)
February 2013 (5)
December 2012 (4)
November 2012 (1)
October 2012 (4)
September 2012 (4)
August 2012 (3)
July 2012 (3)
June 2012 (4)
May 2012 (3)
April 2012 (11)
March 2012 (4)
January 2012 (3)
December 2011 (2)
November 2011 (1)
October 2011 (1)
September 2011 (8)
August 2011 (1)
July 2011 (4)
June 2011 (16)
May 2011 (11)
July 2010 (4)
June 2010 (8)
May 2010 (3)
March 2010 (1)
December 2009 (1)
November 2009 (3)
September 2009 (3)
August 2009 (1)
July 2009 (5)
June 2009 (5)
May 2009 (7)
April 2009 (2)
March 2009 (5)
February 2009 (5)
January 2009 (10)
December 2008 (8)
November 2008 (7)
October 2008 (5)
September 2008 (3)
August 2008 (4)
July 2008 (15)
June 2008 (4)
May 2008 (12)
April 2008 (6)
March 2008 (6)
February 2008 (14)
January 2008 (11)
December 2007 (2)
November 2007 (3)
October 2007 (18)
September 2007 (7)
August 2007 (2)
July 2007 (6)
June 2007 (9)
May 2007 (10)
April 2007 (2)
March 2007 (19)
February 2007 (5)
January 2007 (3)
December 2006 (6)
November 2006 (6)
October 2006 (7)
September 2006 (10)
August 2006 (17)
July 2006 (9)
June 2006 (6)
May 2006 (9)
April 2006 (7)
March 2006 (21)
February 2006 (22)
January 2006 (11)
December 2005 (5)
November 2005 (5)
October 2005 (8)
September 2005 (5)
August 2005 (4)
July 2005 (2)
Michael Freidgeim's OLD Blog
My Blog has been MOVED to
https://mfreidge.wordpress.com
<< Copy HTML documents to Kindle DX
|
Home
|
Does StackOverflow make my blog redundant? >>
LINQ: check for null return from function
Comments
|
Share
One of of my colleagues wrote a LINQ query similar to the following:
var q=from i in keys
select GetTuplesOrPossibleNull(i)
into kvp
orderby kvp.Item2
select kvp;
I’ve noticed, that for some data it throws null exception on kvp.Item2 in orderby statement. It was found that calling function sometimes return null instead of expected key-value pair.
I thought where I should insert check for null and my first idea was to add check in where statement before select function call, something like the following
var q
=
from i
in
keys
where GetTuplesOrPossibleNull(i)!=null
select GetTuplesOrPossibleNull(i)
into kvp
orderby kvp.
Item2
select kvp
;
but it looks ugly, against DRY(don’t repeat yourself) principle and could be double slow, because the same function is called twice.
Fortunately check can be done just before
orderby statement
var q
=
from i
in
keys
select GetTuplesOrPossibleNull(i)
into kvp
where kvp!=null
orderby kvp.
Item2
select kvp
;
It looks much nicer and almost twice quicker. I’ve done some benchmark runs using LinqPad.
void
Main()
{
TimeSpan ts
=
Benchmark(TestCheckingForNullInWhere )
;
ts.
Dump
()
;
//00:00:00.2343750
ts
=
Benchmark(TestCheckingForNullFunctionResult)
;
ts.
Dump
()
;
//00:00:00.4062500
}
void
TestCheckingForNullFunctionResult()
{
int
count
=
20
;
//IEnumerable<Tuple<string, string>> tuples =NotCheckingForNull(count);
IEnumerable
<
Tuple
<
string
,
string
>>
tuples
=
CheckingForNullFunctionResult(count)
;
tuples.
Dump
()
;
}
void
TestCheckingForNullInWhere()
{
int
count
=
20
;
//IEnumerable<Tuple<string, string>> tuples =NotCheckingForNull(count);
IEnumerable
<
Tuple
<
string
,
string
>>
tuples
=
CheckingForNullInWhere(count)
;
tuples.
Dump
()
;
}
public
delegate
void
TestProcedure()
;
//from http://stackoverflow.com/questions/626679/datatable-select-vs-datatable-rows-find-vs-foreach-vs-findpredicatet-lambda
public
TimeSpan Benchmark(TestProcedure tp)
{
int
testBatchSize
=
5
;
List
<
TimeSpan
>
results
=
new
List
<
TimeSpan
>
()
;
for
(
int
i
=
0
;
i
<
testBatchSize
;
i
++
)
{
DateTime start
=
DateTime.
Now
;
tp()
;
results.
Add
(DateTime.
Now
-
start)
;
}
return
results.
Min
()
;
}
// Define other methods and classes here
IEnumerable
<
Tuple
<
string
,
string
>>
NotCheckingForNull(
int
count)
{
List
<
int
>
keys
=
FillListOfInts( count)
;
var q
=
from i
in
keys
select GetTuplesOrPossibleNull(i)
into kvp
orderby kvp.
Item2
select kvp
;
return
q
;
}
IEnumerable
<
Tuple
<
string
,
string
>>
CheckingForNullFunctionResult(
int
count)
{
List
<
int
>
keys
=
FillListOfInts( count)
;
var q
=
from i
in
keys
where GetTuplesOrPossibleNull(i)
!=
null
select GetTuplesOrPossibleNull(i)
into kvp
orderby kvp.
Item2
select kvp
;
return
q
;
}
IEnumerable
<
Tuple
<
string
,
string
>>
CheckingForNullInWhere(
int
count)
{
List
<
int
>
keys
=
FillListOfInts( count)
;
var q
=
from i
in
keys
select GetTuplesOrPossibleNull(i)
into kvp
where kvp
!=
null
orderby kvp.
Item2
select kvp
;
return
q
;
}
List
<
int
>
FillListOfInts(
int
count)
{
var keys
=
new
List
<
int
>
()
;
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
keys.
Add
(i)
;
}
return
keys
;
}
Tuple
<
string
,
string
>
GetTuplesOrPossibleNull(
int
i)
{
int
delay
=
10
;
Thread.
Sleep
(delay)
;
if
(i
%
4
==
0)
return
null
;
else
return
new
Tuple
<
string
,
string
>
(i.
ToString
(),delay.
ToString
()
+
"mc delayed "
+
i.
ToString
())
;
}
Posted on Sunday, June 12, 2011 7:29 AM
LINQ
|
Back to top
Related Posts on Geeks With Blogs
Matching Categories
Unit Test for EF LINQ queries using Mocked DbSet
LINQ
C++/CLI Shell Exercise Code
Linq
LINQ to QuickBooks Data
LINQ
Lightweight Linq style enumerators for Java
linq
IReadOnlyList as a method parameter
LINQ
Comments on this post: LINQ: check for null return from function
No comments posted yet.
Your comment:
Title:
Name:
Email: (never displayed)
(will show your
gravatar
)
Comment:
Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
Verification:
Copyright © Michael Freidgeim | Powered by:
GeeksWithBlogs.net
Popular Posts on Geeks with Blogs
0
Code Monkey Projectiles - Index
Kindle days are gone and amazon is not gonna accept it.
Geeks With Blogs Content Categories
ASP.Net
SQL Server
Apple
Google
SharePoint
Windows
Visual Studio
Team Foundation Server
Agile
Office
Design Patterns
Web
Azure
Brand New Posts on Geeks with Blogs
0
Kindle days are gone and amazon is not gonna accept it.
How do you run multiple instances of Microsoft Teams?
Benefits of Apple Cider Vinegar
Shaking down the Raspberry Pi 400
Blog is Moving