Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Linq Noob, stuck on type inference...

Linq Noob, stuck on type inference...

Scheduled Pinned Locked Moved C#
csharplinqhelprubydatabase
5 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    callingshotgun
    wrote on last edited by
    #1

    I'm trying to wrap my head around using LINQ and lambda expressions in c# 3.0, and as such I threw together a short (presumably) simple exercise- A general function that will take an IEnumerable and return a concatonation of the string representations of each of the elements contained therein. public String concat(ICollection values ) { return values.Cast().Aggregate((Object x, Object y) => x.ToString() + y.ToString()); } However, when I try to run this little gem, I get the following: "The type arguments for method 'System.Query.Sequence.Aggregate, System.Query.Func<t,t,t>)' cannot be inferred from the usage. Try specifying the type arguments explicitly" Googling revealed that this error is usually the result of saying (x,y) => ... instead of explicitly typing X and Y the way you would when defining the parameters in a method. Makes sense, except that I'm explicitly saying "(Object x, Object y)". Further, when I change all instances of "Object" to "String" (which I don't want to do for the final version, as I want this to work for any type of object), it works without a hitch. I don't understand why that makes a difference, since I'm calling ToString() on the object to get the string out, and "ToString()" works for any object. Is this a failing of the type inference system? Or am I just completely glossing over something? Much appreciated.

    L 1 Reply Last reply
    0
    • C callingshotgun

      I'm trying to wrap my head around using LINQ and lambda expressions in c# 3.0, and as such I threw together a short (presumably) simple exercise- A general function that will take an IEnumerable and return a concatonation of the string representations of each of the elements contained therein. public String concat(ICollection values ) { return values.Cast().Aggregate((Object x, Object y) => x.ToString() + y.ToString()); } However, when I try to run this little gem, I get the following: "The type arguments for method 'System.Query.Sequence.Aggregate, System.Query.Func<t,t,t>)' cannot be inferred from the usage. Try specifying the type arguments explicitly" Googling revealed that this error is usually the result of saying (x,y) => ... instead of explicitly typing X and Y the way you would when defining the parameters in a method. Makes sense, except that I'm explicitly saying "(Object x, Object y)". Further, when I change all instances of "Object" to "String" (which I don't want to do for the final version, as I want this to work for any type of object), it works without a hitch. I don't understand why that makes a difference, since I'm calling ToString() on the object to get the string out, and "ToString()" works for any object. Is this a failing of the type inference system? Or am I just completely glossing over something? Much appreciated.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      public string concat(ICollection values)
      {
      return (string)values.Cast().Aggregate((x, y) => x.ToString() + y.ToString());
      }

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      C 1 Reply Last reply
      0
      • L leppie

        public string concat(ICollection values)
        {
        return (string)values.Cast().Aggregate((x, y) => x.ToString() + y.ToString());
        }

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)

        C Offline
        C Offline
        callingshotgun
        wrote on last edited by
        #3

        I'm still getting the "type arguments for method ... cannot be inferred from the usage" error. Even tried throwing gratuitious use of "Object" back in (Object x, Object y). Thanks for the attempt, though.

        L 1 Reply Last reply
        0
        • C callingshotgun

          I'm still getting the "type arguments for method ... cannot be inferred from the usage" error. Even tried throwing gratuitious use of "Object" back in (Object x, Object y). Thanks for the attempt, though.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Member 2148527 wrote:

          I'm still getting the "type arguments for method ... cannot be inferred from the usage" error. Even tried throwing gratuitious use of "Object" back in (Object x, Object y). Thanks for the attempt, though.

          Erm, the code as I pasted I compiled, so either you are missing something, or you are compiling in VS2005 maybe (or using the C# 2.0 compiler?)!

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          C 1 Reply Last reply
          0
          • L leppie

            Member 2148527 wrote:

            I'm still getting the "type arguments for method ... cannot be inferred from the usage" error. Even tried throwing gratuitious use of "Object" back in (Object x, Object y). Thanks for the attempt, though.

            Erm, the code as I pasted I compiled, so either you are missing something, or you are compiling in VS2005 maybe (or using the C# 2.0 compiler?)!

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 alpha 4a out now (29 May 2008)

            C Offline
            C Offline
            callingshotgun
            wrote on last edited by
            #5

            My mistake- I was using a "linq playground" project I'd initially created in VS2005. Moving to 2008 apparently didn't update all the references, so it was still trying to compile down to C# 2.0. Changed the LINQ references to point to 3.5 (instead of the preview release that you could download for 2.0), and changed the "target" to .NET framework 3.5, and it works perfectly now. Sorry for the confusion, and thanks for the help!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups