Tech Off Thread

5 posts

LINQ casting

Back to Forum: Tech Off
  • User profile image
    spivonious

    There has to be a better way to do this...

     

    mBottleCodes = (From r In mDS.BOTTLECODESVIEW Select r.BOTTLECODE Order By BOTTLECODE).OfType(Of String)().ToList()

     

    BOTTLECODE is an Integer field, but I need a list of Strings. (Long story, but poor database design is the main reason)

     

     

  • User profile image
    GamlerHart

    Hi

     

    the .Select-Operation a there for building any projection. So it's easy to transform your Integer to a String by adding a .ToString() in your Select-Statement:

     

    mBottleCodes = (From r In mDS.BOTTLECODESVIEW

                                   Select r.BOTTLECODE.ToString()
                                    Order By BOTTLECODE).ToList()

  • User profile image
    spivonious

    GamlerHart said:

    Hi

     

    the .Select-Operation a there for building any projection. So it's easy to transform your Integer to a String by adding a .ToString() in your Select-Statement:

     

    mBottleCodes = (From r In mDS.BOTTLECODESVIEW

                                   Select r.BOTTLECODE.ToString()
                                    Order By BOTTLECODE).ToList()

    Hey thanks for the reply. I tried what you wrote and the compiler complains "Range variable name cannot match the name of a member of the 'Object' class." and it underlines the ToString() piece.

     

    edit: Put parens around r.BottleCode.ToString() and it worked. Thanks!

  • User profile image
    figuerres

    spivonious said:
    GamlerHart said:
    *snip*

    Hey thanks for the reply. I tried what you wrote and the compiler complains "Range variable name cannot match the name of a member of the 'Object' class." and it underlines the ToString() piece.

     

    edit: Put parens around r.BottleCode.ToString() and it worked. Thanks!

    By the way in case this helps anyone -

    two tools i use in working with LINQ and SQL

    Linqpad from www.linqpad.net

    and Linqer from www.sqltolinq.com

     

    together they rock for all kinds of back and forth to get a linq query to work right or to see how to get from sql to linq

    and to debug what sometimes goes wrong in them.

     

    linqer sometimes makes poor choises about how to handle some types so i do re-work it a bit. it may use a Convert method when it gets stuck on a few things... but it does get the basics of transforming a sql join and group into valid linq syntax.

    just check it with linqpad and tweak the bad bits.... bam!

     

  • User profile image
    ScanIAm

    spivonious said:
    GamlerHart said:
    *snip*

    Hey thanks for the reply. I tried what you wrote and the compiler complains "Range variable name cannot match the name of a member of the 'Object' class." and it underlines the ToString() piece.

     

    edit: Put parens around r.BottleCode.ToString() and it worked. Thanks!

    You can also use ConvertAll<Type>(...) to convert a collection from one type to another.  I find myself using this a lot in .net 3.5 when I have a list of <T> and T implements interface I, and I need a list of <I>.

     

    I anxiously await .net 4.0 Smiley

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.