Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

XML Feeds

Tags: linq

All the LessThanDot Journals

VB.Net: Adding ContainsAny and ContainsAll to ICollection(of T)

by chrissie1


Permalink 09 Sep 2008 03:52 , Categories: Microsoft Technologies, VB.NET Tags: extension methods, linq, vb.net

In an ICollection(of T) you have a contains method to see if your collection has the requested element in it. But if you want to look for Multiple elements, you have to resort to making predicates (and we all know how ugly those get).

So why didn’t MS implement ContainsAny (OR) and/or ContainsAll (AND). I couldn’t think of a good reason, so I made them myself. They are perhaps a bit over easy and not very performance friendly but they work.

Here are the extension methods.

  1. <Extension()> _
  2.     Public Function ContainsAny(Of T)(ByVal collection As ICollection(Of T), ByVal Parameters As ICollection(Of T)) As Boolean
  3.         For Each parameter In Parameters
  4.             If collection.Contains(parameter) Then
  5.                 Return True
  6.             End If
  7.         Next
  8.         Return False
  9.     End Function
  10.  
  11.     <Extension()> _
  12.     Public Function ContainsAll(Of T)(ByVal collection As ICollection(Of T), ByVal Parameters As ICollection(Of T)) As Boolean
  13.         For Each parameter In Parameters
  14.             If Not collection.Contains(parameter) Then
  15.                 Return False
  16.             End If
  17.         Next
  18.         Return True
  19.     End Function

They are generic, so you can use them in most places.

Here is an example how it works.

  1. Imports System.Runtime.CompilerServices
  2. Imports System.Net.NetworkInformation
  3.  
  4. Module Module1
  5.  
  6.     Sub Main()
  7.         ShowNetworkInterfaces()
  8.         Console.ReadLine()
  9.     End Sub
  10.  
  11.     Public Sub ShowNetworkInterfaces()
  12.         Dim computerProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
  13.         Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
  14.         If nics Is Nothing OrElse nics.Length < 1 Then
  15.             Console.WriteLine("  No network interfaces found.")
  16.             Exit Sub
  17.         End If
  18.         For Each adapter As NetworkInterface In nics
  19.             Dim properties As IPInterfaceProperties = adapter.GetIPProperties()
  20.             Console.WriteLine(adapter.Name)
  21.             For Each ip As System.Net.IPAddress In properties.DnsAddresses
  22.                 Console.WriteLine("DNS server : {0}", ip.ToString)
  23.             Next
  24.         Next
  25.         Dim _list As New List(Of System.Net.IPAddress)
  26.         _list.Add(New System.Net.IPAddress(New Byte() {192, 168, 1, 2}))
  27.         _list.Add(New System.Net.IPAddress(New Byte() {192, 168, 1, 3}))
  28.         Dim result = From n In nics Where n.GetIPProperties.DnsAddresses.ContainsAny(_list)
  29.         Console.WriteLine(result.Count)
  30.         result = From n In nics Where n.GetIPProperties.DnsAddresses.ContainsAll(_list)
  31.         Console.WriteLine(result.Count)
  32.         Console.WriteLine()
  33.     End Sub
  34.  
  35.     <Extension()> _
  36.     Public Function ContainsAny(Of T)(ByVal collection As ICollection(Of T), ByVal Parameters As ICollection(Of T)) As Boolean
  37.         For Each parameter In Parameters
  38.             If collection.Contains(parameter) Then
  39.                 Return True
  40.             End If
  41.         Next
  42.         Return False
  43.     End Function
  44.  
  45.     <Extension()> _
  46.     Public Function ContainsAll(Of T)(ByVal collection As ICollection(Of T), ByVal Parameters As ICollection(Of T)) As Boolean
  47.         For Each parameter In Parameters
  48.             If Not collection.Contains(parameter) Then
  49.                 Return False
  50.             End If
  51.         Next
  52.         Return True
  53.     End Function
  54. End Module

And this is the result:

Local area connection
DNS server : 192.168.1.2
DNS server : 192.168.1.3
Local area connection 2
DNS server : 192.168.1.2
DNS server : 192.168.1.3
Local area connection 3
DNS server : 192.168.1.2
DNS server : 192.168.1.3
3
0

These are not the exact results for my machine but you get the point.

It seems to work ;-)

Leave a comment »Send a trackback » 351 views

VB.Net: Linq and I keep forgetting

by chrissie1


Permalink 09 Sep 2008 01:33 , Categories: Microsoft Technologies Tags: linq, vb.net

Problem

I keep forgetting that it needs the System.linq namespace to work. And I keep forgetting that it is not set by default on my projects. I try not to overuse linq but sometimes it is easy.

So what happened?

I was trying this:

  1. Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
  2. Dim result = From e In nics


The solution

And it came up with this error and a squiggly line under nics.

Expression of type ‘1-dimensional array of System.Net.NetworkInformation.NetworkInterface’ is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider.

Hmm, not very helpful. What it is trying to say is that I have to add
Imports System.Linq to the file and then it works. Why can’t it just say that instead of that cryptic message? I will be so bold to suggest a rewrite of that error message.

Expression of type ‘1-dimensional array of System.Net.NetworkInformation.NetworkInterface’ is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the linq provider. This means add a reference to System.Core and an Imports System.Linq, have fun.

Or, of course, they could just do it for me, what good is an IDE if it can’t even solve the obvious mistakes?

Leave a comment »Send a trackback » 164 views

LINQ to SQL queries involving strings cause SQL Server procedure cache bloat

by SQLDenis


Permalink 28 Aug 2008 10:12 , Categories: Data Modelling & Design Tags: bug, linq, performance

Adam Machanic created an item on connect explaining how LINQ to SQL queries involving strings cause SQL Server procedure cache bloat

If an application is using LINQ to SQL and the queries involve the use of strings that can be highly variable in length, the SQL Server procedure cache will become bloated with one version of the query for every possible string length. For example, consider the following very simple queries created against the Person.AddressTypes table in the AdventureWorks2008 database:

  1. var p =
  2.                 from n in x.AddressTypes
  3.                 where n.Name == "Billing"
  4.                 select n;
  5.  
  6.             var p =
  7.                 from n in x.AddressTypes
  8.                 where n.Name == "Main Office"
  9.                 select n;

If both of these queries are run, we will see two entries in the SQL Server procedure cache: One bound with an NVARCHAR(7), and the other with an NVARCHAR(11). Now imagine if there were hundreds or thousands of different input strings, all with different lengths. The procedure cache would become unnecessarily filled with all sorts of different plans for the exact same query. Even worse, imagine if a query used two or three different string parameters. The procedure cache could end up with hundreds of thousands or even millions of entries, the only differences being the variable lengths

Wow that is bad indeed, please go to the connect site and vote for this so that Microsoft ‘fixes’ this. Here is the URL: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363290

4 comments »Send a trackback » 1162 views

.NET Framework 3.5 SP1: LINQ perf improvements (LINQ to Objects and LINQ to SQL)

by SQLDenis


Permalink 12 Aug 2008 06:56 , Categories: Data Modelling & Design Tags: linq, performance

Dinesh wrote a blogpost about .NET Framework 3.5 SP1: LINQ perf improvements (LINQ to Objects and LINQ to SQL). There are three perf improvements in the just released SP1

Specialized enumerable: The new implementation recognizes queries that apply Where and/or Select to arrays or List<T>s and fold pipelines of multiple enumerable objects into single specialized enumerables. This produces substantial improvement in base overhead of common LINQ to Objects queries (at times 30+%).

Make sure to check it out!

Leave a comment »Send a trackback » 528 views

SQL Server 2008 geography data type screencasts on Channel 9

by SQLDenis


Permalink 19 Jul 2008 14:38 , Categories: Data Modelling & Design Tags: linq, screencast, sql server 2008, wcf

Channel 9 has two screencast that deal with the new geography data type in SQL Server 2008.

Saving Virtual Earth Polygons to SQL Server 2008

Marc Schweigert shows you how to draw a polygon on a Virtual Earth map and save it using ASP.NET AJAX, Windows Communication Foundation (WCF), LINQ to SQL, and the new geography data type in SQL Server 2008.

Rendering Polygons from SQL Server 2008 on Virtual Earth

Marc Schweigert builds off of the concepts shown in his previous screencast and shows you how to render a polygon on a Virtual Earth map using REST, Windows Communication Foundation (WCF), LINQ to SQL, and the new geography data type in SQL Server 2008.

Enjoy the shows, and to learn more about the GeoRSS utility library, visit:
http://blogs.msdn.com/eugeniop/archive/2008/07/01/simple-georss-utility-library-released.aspx

Leave a comment »Send a trackback » 617 views