Cast Question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mironline
    New Member
    • May 2008
    • 24

    Cast Question.

    Dear Friend .
    I have a question about casting in C# . I would highly appreciate if someone could help me.

    Code:
    var t = (from o in baseContext.Myuser[B]s[/B]
    where o.name == "name"
    select o);
    
    object.datasource = t;
    in object selectedindexch ange event :

    Code:
    var address = (object.selecteditem as [B]Myuser)[/B].address;

    Now I want to select like this

    Code:
    var t = (from o in baseContext.Myuser[B]s[/B]
    where o.name == "name"
    select new {mynewName = o.name + " *** " + o.family , o.address , o.age );
    
    object.datasource = t;
    the question is :
    how can get mynewName value in selectedindexch ange event .

    best.
    ali
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    Hi,

    I don't know, which class your "object" is of, but maybe there is a ValueMember or DisplayMember property.
    If you set the ValueMember-property to "mynewName"
    Code:
    object.ValueMember = "mynewName"
    you could use it in the SelectedIndexCh anged-EventHandler
    Code:
    object.SelectedValue

    Comment

    • mironline
      New Member
      • May 2008
      • 24

      #3
      dear ChBinder
      thank you for your replay.

      my problem is : who to access the object's datasource on slectedIndexCha nged
      event.

      your solution is the good way to get the 'mynewName' value , but how could I get the combination of "mynewName" and "address" ?

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        Hi,

        the problem is, you are using an anonymous type as the result of your select-statement.
        If you would use a class defined by yourself, it would make it easier.

        Code:
        void xyz {
          var t = (from user in baseContext.Myusers
            where user.name == "name"
            select new NewMyUser(o.name + " *** " + o.family , o.address , o.age)).ToArray();  // This returns NewMyUser[]
        
          object.DataSource = t;
          object.DisplayMember = "MyNewName"; // Show MyNewName to User
          
        }
        
        class NewMyUser {
          public NewMyUser(string myNewName, string address, int age) {
            MyNewName = myNewName;
            Address = address;
            Age = age;
          }
        
          public string MyNewName { get; set; }
          public string Address { get; set; }
          public int Age { get; set; }
        }
        In the SelectedIndexCh anged-Event you can cast the SelectedItem and access the properties

        Code:
        (object.SelectedItem as NewMyUser).Age // or  .Address ...

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          This is very strange looking C#.
          I am guessing those are linq queries?

          Comment

          • Christian Binder
            Recognized Expert New Member
            • Jan 2008
            • 218

            #6
            Originally posted by Plater
            This is very strange looking C#.
            I am guessing those are linq queries?
            Yes, the creation of the DataSource (resp. the filtering of the original data) is done via LinQ. But the main problem here is that it's not possible (without using of Reflection imho) to access properties of anonymous types.

            Comment

            Working...