1

I want to use a Linq query to calculate the length of strings and return only those which are greater than 7 characters long. The following code works well for "strings":

public IEnumerable<string> LengthOfNames()
    {
        this._context.ContextOptions.LazyLoadingEnabled = false;
        var Query = from c in this._context.CustomerInfoes
                    where c.CustomerName.Length > 7
                    orderby c.CustomerName.Length 
                    select c.CustomerName;
        return Query.ToList();
    }

But, when I use a similar query for "Integers", I get an error " 'int' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument etc etc.." Here's the code:

        public IEnumerable<int> LengthOfNumbers()
    {
        this._context.ContextOptions.LazyLoadingEnabled = false;
        var Query = from c in this._context.CustomerInfoes
                    where c.ContactNo.Length > 7
                    orderby c.ContactNo.Length
                    select c.ContactNo;
        return Query.ToList();
    }

As an alternative, I tried this:

public IEnumerable<int> GreaterThanSeven()
    {
        this._context.ContextOptions.LazyLoadingEnabled = false;
        var Query = from c in this._context.CustomerInfoes
                    where c.ContactNo > 9999999
                    orderby c.ContactNo
                    select c.ContactNo;
        return Query.ToList();
    }

Which works just fine. My question is : Is this the correct (or the only) way to calculate the length of a numeric string?

  • Can you please define "numeric string" or at least show an example of? Something like "000333"? – Alexei Levenkov Apr 26 '14 at 08:01
  • 3
    If you've got integers, then you're not calculating the lengths of strings... you're calculating the number of decimal digits in the number. (If you represent the same number in hex for example, you'd get different boundaries.) It may be worth thinking more about *why* you're trying to do this. – Jon Skeet Apr 26 '14 at 08:02
  • When the Length matters, `int` may not be the right type for `ContactNo`. – H H Apr 26 '14 at 08:33
  • @HenkHolterman I am learning to code in `Linq`, so this question was asked more out of curiosity. The variable `ContactNo` contains dummy data. But yes, thanks for the suggestion. – MarlboroMan_wykyu Apr 26 '14 at 09:40
  • @AlexeiLevenkov As far as the example is concerned, it can be anything like `"1234567"` or `"080801234"` – MarlboroMan_wykyu Apr 26 '14 at 09:42

1 Answers1

1

Your query (i.e. where c.ContactNo > 9999999 ) is correct and efficient, but also you can run this query

public IEnumerable<int> GreaterThanSeven()
    {
        this._context.ContextOptions.LazyLoadingEnabled = false;
        var Query = from c in this._context.CustomerInfoes
                    where SqlFunctions.StringConvert((double)c.ContactNo).Length > 7
                    orderby c.ContactNo
                    select c.ContactNo;
        return Query.ToList();
    }
Reza
  • 18,865
  • 13
  • 88
  • 163
  • `.ToString().Length > 7' create as [non-sargable](http://stackoverflow.com/questions/799584/what-makes-a-sql-statement-sargable) query, not a good idea. – Erik Philips Apr 26 '14 at 08:40
  • @ErikPhilips Yep, I mentioned that in my answer – Reza Apr 26 '14 at 08:47
  • @RezaRahmati I tried using `ToString().Length` in my code. It throws this exception `"LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."` – MarlboroMan_wykyu Apr 26 '14 at 10:12
  • @MarlboroMan_wykyu SqlFunctions is located in System.Data.Objects.SqlClient namespace and is used to have some basic functionality in linq queries. http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions%28v=vs.110%29.aspx – Reza Apr 26 '14 at 12:05
  • @RezaRahmati I see. I asked because it gives me an error saying `"The name 'SqlFunctions' does not exist in the current context"` – MarlboroMan_wykyu Apr 26 '14 at 12:17
  • @MarlboroMan_wykyu is it compile time error or runtime? – Reza Apr 26 '14 at 12:22
  • @MarlboroMan_wykyu Did you add this namespace `System.Data.Objects.SqlClient` to your class? – Reza Apr 26 '14 at 12:28
  • @RezaRahmati Thank you for pointing that out. The errors have all gone away. But when I executed the program, it returns all the strings present in the variable `ContactNo`. and If I try to return numbers that are less than 7 digits long, it returns me nothing. Which means it is comparing all the strings to the number 7 itself, and returning values that are greater than 7 (instead of the length). – MarlboroMan_wykyu Apr 28 '14 at 04:50
  • @MarlboroMan_wykyu I don't thinks so, please run a sql profiler and see what is the translated query in sql. – Reza Apr 28 '14 at 05:58
  • @RezaRahmati Okay, I'll work on it now. Thanks for all the help! – MarlboroMan_wykyu Apr 28 '14 at 06:35