LINQ to Sql and .Contains() trap.

by aristosamar 6. July 2009 00:00

I went in to a trouble some time back playing with LINQ.
What kind of trouble am I talking about?

Lets see the code:

            int[] values = { 1, 4, 6, 3 };

            foreach (var item in values)
            {
                var query = (from r in dc.MarketCodes
                            where r.ID == item
                            select r).Single();

     //... do something with the result
   }

It's a typical example of querying a database using a table of indexes.
Nice, easy and clean.

Having reading a programming forum I’ve got an advice to rather use this example:

            int[]values = { 1, 4, 6, 3 };

            var query = from r in dc.MarketCodes
                        where values.Contains(r.ID)
                        select r;


Is it not fantastic? I think it really, really is.
I loved it since I have seen it first time. It's shorter, doesn't require a loop and it looks very sexi.

Unfortunately using .Contains() may put us in trouble because of the way the C# compiler will translate the LINQ query to T-SQL.

The content of the “where values.Contains(r.ID)” will be translated to something more or less looking like this:

“where r.ID IN(@p1, @p2, @p3…)”

It doesn’t look bad, just standard T-SQL which will be passed to the database engine.
It has some hidden limitations I’ve discovered when my application crashed after passed large amount of values in the “values” table.

I went to google for help and found that T-SQL has limitations about the amount of parameters in the query. When trying to pass more than 2100 parameters we will get an exception:

The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Too many parameters were provided in this RPC request. The maximum is 2100.

Unfortunately I cannot tell how many values will be in the “parameters table” at every time in my application, which forces me to use less pretty (first) version of the query again.

Other words, it is nice to use LINQ and its syntax but this same it’s good to know how C# compiler will translate it to T-SQL.

.Contains() looks cool but it can be dangerous!

APasswordSafe released.

by aristosamar 10. September 2008 09:55

The desktop version as well as mobile one has been released.

Both are ready to be downloaded from http://www.aristo-samar.com/pc.en.aspx

Any comments are appreciated :-)

APasswordSafe private Beta

by aristosamar 5. August 2008 15:36

I was busy developing a commercial project called AroraTool for my company for last six months.

I decided to take few days breake and develop something else.

Freeware apps that I can share with others for free is what always turns me on. I have spent few days writing:

The APasswordSafe is a C#.NET (Microsoft .Net Framework 2.0 required) application which purpose is to store highly confidential information like web-site passwords, bank details, pins etc. in one safe place.

To encrypt this all information The APasswordSafe uses TripleDES algorithm with (forced by application) strong master-password as an encryption key.

I have heard an IT-Conversations presentation about the DES algorithm. A few facts which I found worth to mention are:

1. People often use short one-word passwords.

2. English disctionary consists about 411 000 words

3. To breake one-word-english password encrypted by DES using brut-force takes about 20min for average laptop.

4. A long and strong password with embedded digits and capital letters is... practicaly unbreakable by brut-force techniques.

I've decided to use TripleDES algorithm and force an user to use minimum 12 characters master-password with minimum one digit and one capital letter.

It gives strong security and ensure that user's data is safe...

 

In few days from now, when we will finish beta testing of this app I will publish more about it here.

LINQ - Passing query results

by aristosamar 22. December 2007 06:11

There is something in C# I am crazy about last time. There really is.

It is the LINQ, which stands for Language INtegrated Query.LINQ came up along with C# 3.0 and .Net Framework 3.5.I really enjoy it...

but...

Have a look at this code:

 

It works ok unless we want to pass the result out from the Fo method by some way.

Using var query this trick is not possible, because of an unknown type (maybe rather dynamic type) the var will converted into on the compile time.

It is not possible to pass var query into a method too because of the same reason.It looks to me like the biggest disadvantage of LINQ.The fact that we are unable to pass var into and pass it out of the method means that everything we want to do with the query result has to be done within the Fo() method.

It is not very convenient I would say.
I would rather think about something like this:

 

This would be nice but it wont work. The type of var query is dynamically created by compiler which takes a look at the syntax on the right side of = sign.
Trying to declare var query as we have done in the last example, will give no chance for compiler to figure out the type of the var.

I was looking for the solution and I have found it somewhere on the net.So, to be able to pass the result whether out or into the method, or even declare our variable outside the method and use it within it we can use IEnumerable , which is in System.Collections.Generic namespace.

Lets try something like that:

 

Right now your result is accessible from any part of your class.
It can be also passed into and out of the method.

It made me happy about LINQ again...

Tags: ,

C#

Powered by BlogEngine.NET 1.4.0.0
Theme by Mads Kristensen

About the author

I give up things to be able to code and create virtual worlds... because I have a passion... I don't want to let it go... and I am greateful for it...

Recent comments

Comment RSS

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's or anyone view in  anyway.

© Copyright (C) 2008 Mariusz Zaleski