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 extension method and referencing object?

Linq extension method and referencing object?

Scheduled Pinned Locked Moved C#
csharplinqquestionannouncement
7 Posts 3 Posters 0 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.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    Is there a good looking way of receiving an object reference instead of a new object with the linq extension methods? Id like the two code lines below to update the object within the salesLst.

    // List salesLst
    ProdTotalSales ps = salesLst.First(p => p.Month == o.OrderDatum.Month);
    ps.Antal += r.BestSaldo;

    OriginalGriffO 1 Reply Last reply
    0
    • T TMattC

      Is there a good looking way of receiving an object reference instead of a new object with the linq extension methods? Id like the two code lines below to update the object within the salesLst.

      // List salesLst
      ProdTotalSales ps = salesLst.First(p => p.Month == o.OrderDatum.Month);
      ps.Antal += r.BestSaldo;

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      It does! :laugh:

      public class ProdTotalSales
      {
      public string Text { get; set; }
      public int Value { get; set; }
      public ProdTotalSales(string s, int i)
      {
      Text = s;
      Value = i;
      }
      public override string ToString()
      {
      return string.Format("{0}:{1}", Value, Text);
      }
      }

      ProdTotalSales p1 = new ProdTotalSales("P1", 1);
      ProdTotalSales p2 = new ProdTotalSales("P2", 2);
      List list = new List() { p1, p2 };
      Console.WriteLine("Before:");
      foreach (ProdTotalSales p in list) Console.WriteLine(p);
      ProdTotalSales pn = list.First(p => p.Value == 2);
      pn.Text = "P2 Modified";
      Console.WriteLine("After:");
      foreach (ProdTotalSales p in list) Console.WriteLine(p);
      

      Before:
      1:P1
      2:P2
      After:
      1:P1
      2:P2 Modified

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      T 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        It does! :laugh:

        public class ProdTotalSales
        {
        public string Text { get; set; }
        public int Value { get; set; }
        public ProdTotalSales(string s, int i)
        {
        Text = s;
        Value = i;
        }
        public override string ToString()
        {
        return string.Format("{0}:{1}", Value, Text);
        }
        }

        ProdTotalSales p1 = new ProdTotalSales("P1", 1);
        ProdTotalSales p2 = new ProdTotalSales("P2", 2);
        List list = new List() { p1, p2 };
        Console.WriteLine("Before:");
        foreach (ProdTotalSales p in list) Console.WriteLine(p);
        ProdTotalSales pn = list.First(p => p.Value == 2);
        pn.Text = "P2 Modified";
        Console.WriteLine("After:");
        foreach (ProdTotalSales p in list) Console.WriteLine(p);
        

        Before:
        1:P1
        2:P2
        After:
        1:P1
        2:P2 Modified

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        T Offline
        T Offline
        TMattC
        wrote on last edited by
        #3

        Well, I made the classic mistake of attaching too little code to my question, sorry about that. The thing is that my ProdTotalSales was declared as a struct. That meant that when I was debugging, the debugger showed ps.Antal=1 but salesLst[0].Antal=0 after executing the two rows of code above. I still dont understand how that could be. Now when I changed the ProdTotalSales into being a class, it works just fine. I would really appreciate an explanation to this. Could it have something to do with me calling the default-constructor? The ProdTotalSales looked like this:

        public struct ProdTotalSales
        {
            public int Year { get; set; }
            public int Month { get; set; }
            public int Antal { get; set; }
        
            public ProdTotalSales(int \_year, int \_month, int \_antal) : this()
            {
                Year = \_year;
                Month = \_month;
                Antal = \_antal;
            }
        }
        
        OriginalGriffO S 2 Replies Last reply
        0
        • T TMattC

          Well, I made the classic mistake of attaching too little code to my question, sorry about that. The thing is that my ProdTotalSales was declared as a struct. That meant that when I was debugging, the debugger showed ps.Antal=1 but salesLst[0].Antal=0 after executing the two rows of code above. I still dont understand how that could be. Now when I changed the ProdTotalSales into being a class, it works just fine. I would really appreciate an explanation to this. Could it have something to do with me calling the default-constructor? The ProdTotalSales looked like this:

          public struct ProdTotalSales
          {
              public int Year { get; set; }
              public int Month { get; set; }
              public int Antal { get; set; }
          
              public ProdTotalSales(int \_year, int \_month, int \_antal) : this()
              {
                  Year = \_year;
                  Month = \_month;
                  Antal = \_antal;
              }
          }
          
          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Ah! As you say, too little code! :laugh: The thing is that a struct is always a Value type, and a class a Reference type - which means that when you assign a struct instance to another variable you ***always*** get a copy, not a reference to the existing one - and it doesn't matter if you do it directly:

                  ProdTotalSales pn = list\[0\]
          

          Or via a Linq method:

                  ProdTotalSales pn = list.First(p => p.Value == 2);
          

          You always get a copy of the data, not a reference to the original item. I wrote an article on the difference last year: Using struct and class - what's that all about?[^] which may help.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          T 1 Reply Last reply
          0
          • T TMattC

            Well, I made the classic mistake of attaching too little code to my question, sorry about that. The thing is that my ProdTotalSales was declared as a struct. That meant that when I was debugging, the debugger showed ps.Antal=1 but salesLst[0].Antal=0 after executing the two rows of code above. I still dont understand how that could be. Now when I changed the ProdTotalSales into being a class, it works just fine. I would really appreciate an explanation to this. Could it have something to do with me calling the default-constructor? The ProdTotalSales looked like this:

            public struct ProdTotalSales
            {
                public int Year { get; set; }
                public int Month { get; set; }
                public int Antal { get; set; }
            
                public ProdTotalSales(int \_year, int \_month, int \_antal) : this()
                {
                    Year = \_year;
                    Month = \_month;
                    Antal = \_antal;
                }
            }
            
            S Offline
            S Offline
            Simon_Whale
            wrote on last edited by
            #5

            after looking at this MSDN: Struct[^] is shows that struct is a value type hence how it behaves different to a class in this situation. way around your issue I am not sure at the moment.

            Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Ah! As you say, too little code! :laugh: The thing is that a struct is always a Value type, and a class a Reference type - which means that when you assign a struct instance to another variable you ***always*** get a copy, not a reference to the existing one - and it doesn't matter if you do it directly:

                      ProdTotalSales pn = list\[0\]
              

              Or via a Linq method:

                      ProdTotalSales pn = list.First(p => p.Value == 2);
              

              You always get a copy of the data, not a reference to the original item. I wrote an article on the difference last year: Using struct and class - what's that all about?[^] which may help.

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              T Offline
              T Offline
              TMattC
              wrote on last edited by
              #6

              Yes, should have thought of that. I know the struct is a value typ, though didnt think of the implications of it. Thanks for the explanation, and Ill take a peek at your article.

              OriginalGriffO 1 Reply Last reply
              0
              • T TMattC

                Yes, should have thought of that. I know the struct is a value typ, though didnt think of the implications of it. Thanks for the explanation, and Ill take a peek at your article.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                TMattC wrote:

                didnt think of the implications of it.

                You never do until it bites you! :laugh:

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                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