fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Anonymous Types - syntax needed

Anonymous Types - syntax needed 2 years 5 months ago #1

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Hi Team,

I am just starting out on my quest to prepare suitable materials on LINQ for the Cologne Conference on xBase in April. I am using as a base a huge amount of good stuff I did way back using C#.

I have made my first successful steps in VS 2015 with the latest XSharp compiler, and now wish to go further.

I need to be able to specify and use anonymous types as shown in the C# code in the attachments - PLEASE help me move forward.

Its the 'new { .... }' stuff that I need help with.

Best Regards,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 5 months ago #2

Phil,

Our syntax for an anonymous type is:
VAR o := CLASS { Name := "test", Value := "something" }
and in combination with LINQ:
VAR         result := from c in db.Customers where c.Orders.Count > 0 ;
 select CLASS{ ID := C.CustomerID, Name := C.CustomerName, OrderCount := C.Orders.Count}

I know our documentation is far from ready, but this is listed in the "New Features" branch in the TOC.

Robert
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 5 months ago #3

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Thanks a bunch Robert,

I knew you would put me straight ;-0) My post was NOT a complaint of any sort.

I am a little (a lot?) 'rusty' after 6 months 'off' and so I may well have missed something somewhere - sorry !

I will give this a go with what I have done so far today. Things are coming on nicely.

Fingers Crossed,
Phil.

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 5 months ago #4

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Robert,

Just a quick post to celebrate my success with your suggested syntax ;-0) Most appreciated.



For any guys trying LINQ for themselves at the moment (before Cologne 2017) I will keep posting new stuff as I get more success.

There is only any point in using LINQ if we can use a wide range of query syntaxes and formats. It took me a long time and a lot of effort to crack the C# stuff, so there is still a way to go for me (and you).

Surprisingly, the eBook available at the time were not very good, confused, incomplete, and di not use the syntax recommended by Microsoft - they say to use lambda expressions only when the 'simple' syntax is not available for use. So this is what I did.

Watch this space.

Kindest regards,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 5 months ago #5

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Hi Robert,

I have Anonymous Types (ATs) working as you indicated in the syntax you posted (thanks), however, it looks to me as if C# has one format of ATs which allows the programmer to leave out the name of the property, and the compiler then just uses the same property name as the item being chosen. This is extremely useful (handy) and without it there are lots of example which will be difficult to convert to X#.

I am including a few working samples from my 'Click Start' eNotes of August 2012 - yes, as long ago as over four years.

Check out lines 190, 2620, and 97.

Please can you help me ?
Regards,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 5 months ago #6

Phil,

In the current build we only support anonymous types that have a Name := Value syntax ( we had totally forgotten about the other syntax).

I have just made a change to the compiler to also support the unnamed syntax.

The following code compiles and runs fine on my development machine.
As you can see I am no longer providing a name for the LastName property.
FUNCTION Start AS VOID
   VAR oPhil := Phil{}
   VAR oSon  := CLASS{ FirstName:="Bob", oPhil:LastName, Age:=42}
   ? oSon:FirstName, oSon:LastName, oSon:Age
   Console.Read()


CLASS Phil
   PROPERTY FirstName AS STRING AUTO
   PROPERTY LastName  AS STRING AUTO
   CONSTRUCTOR()
      FirstName := "Phil"
      LastName  := "HepBurn"
END CLASS
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 5 months ago #7

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Well done 'indeed' Robert ;-0)

That's what I call "responsive".

This is a great addition to ATs. When you have a version of the X# compiler suitable for a standard guy like me to try out, then I will see if all my stuff works against the new addition(s).

As a matter of interest to me, working on the session material for April (if accepted by Michael and Meinhard), am I correct in assuming that Intellisence for LINQ is not yet available to us - neither in VS nor XIDE. I have had a lot to check-out over the last couple of days and have not yet tried XIDE to see what it can do with LINQ intellisense.

Yes, we can live and work without Intellisense - BUT - it would be nice to have sometime, possibly before the April conference.

Best regards,
Phil.

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 5 months ago #8

Phil,
Intellisense for AT is on the list.
I think it will be there once we get the intellisense based on the Roslyn intellisense engine up and running.

I can't say when that will happen, but April sounds doable. Probably before that.

Robert
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 4 months ago #9

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Hi again Robert (and Team),

I have had some good success with my XSharp LINQ coding, and session material preparation, since we last posted. Even doing a few things in X# I never got around to in C# some years back, so I must be making good progress.

However, I do need some help with syntax on accessing anonymous types (ATs) once I have wrapped them into Object 'Lists' for ease of movement from method to method, and data binding as well.

The attached details are from a simple WPF app which has hard coded data items - using a simple business class called 'Person' - I would like to "un-wrap" the collection items from Objects in a List to the original AT (or similar) so that I can access the properties therein.

Please can you help me and give me some advice - this is not the stuff which is my experience, or knowledge .... HELP !!!

Cheers,
Phil. P.S. please inform me if you need any more information on what I am trying to do.

Attachments:

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 4 months ago #10

Phil,
I am not sure if you what you want is even possible in C#. Try the following example:
FUNCTION Start AS VOID
	
	VAR o := CLASS{FirstName := "Phil", LastName := "Hepburn"}
	? o           
	? o:GetType()
	Console.Read()

The output is:
{ FirstName = Phil, LastName = Hepburn }
<>f__AnonymousType0`2[System.String,System.String]

You can see that the compiler generates a type with 2 strings. Its name is determined by the (Roslyn) compiler.
If you move this to an object list then the only thing that you know is that there are 2 properties: FirstName and LastName

The only thing that I have seen in other peoples codes is not to store the AT in a List of objects, but in a list of Dynamics:
Add the following to the example code above (and add using System.Collections.Generic to the top)
	VAR list := List<DYNAMIC>{}
	list:Add(o)              
	FOREACH VAR element IN list
		? element:FirstName, element:LastName
	NEXT
	Console.Read()

Robert
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 4 months ago #11

Hi Phil,

If you're going to be moving this data around and using it elsewhere, surely it would be better to project into a concrete type and have done with it.

You could simply create classes to represent these subsets of data, and then you can have everything strongly typed and under control.

Nick

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 4 months ago #12

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Thanks Robert (Nick and all),

This has done the trick. Even though at present I seem to have to use an enumeration (FOREACH) when I would prefer a "results.ToList()" assignment.

DYNAMIC seems to be the way to go - I will invest more time tomorrow.

Yes, Nick (next post) it would be best to make our own classes and in fact when we move to "LINQ to SQL" and "LINQ to Entities" things are simpler because we tend to have our classes.

If we are to have and use Anonymous Types then it is good to have some measure of control over them and their use ;-0)

Cheers,
Thanks to you all for your help,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 4 months ago #13

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Hi Nick,

I tend to agree with your thoughts and ideas, and have expressed such ideas in my C# eNotes some years back.

However, ATs (anonymous types) do have some uses and therefore it is nice to feel that we can get the most out of them if and when we wish.

Well, for 'most' I mean a bit more then them being in control of me / us !!!

Thanks for your input, I will need much more advice as the session material develops.

Next stop is "LINQ to SQL' then "LINQ to Entities".

Cheers,
Phil.

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 4 months ago #14

I really dislike the idea of using Dynamic types.... if you're just using the anonymous type inside a single method, then that's fine to keep it anonymous, as you have all the info you need to understand what's going on right in front of you.

But as soon as you start to return info from a method, I think you should really go for a known concrete type, unless there's a really good reason not to. Otherwise to use the returned data, you either have to use reflection (very slow and inefficient, and horrible to understand the resulting code), or else you have to go and look at the called method to interpret the data, which makes it difficult to write reliable, maintainable code.

Just my 2p.

Nick

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 4 months ago #15

Hi Phil, posted at the same time you were making your points about anonymous types... anyway at least you know what I think about them now!!!

Nick

Please Log in or Create an account to join the conversation.

Anonymous Types - syntax needed 2 years 4 months ago #16

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Hi Nick,

Now I have done some more research and testing I see just what you mean about DYNAMICS. I have the syntax working quite satisfactorily - BUT !!!

Because the DYNAMIC type is 'by-passed' for checking by the compiler then the properties are not then available for intellisense in other places (or anywhere) in our code being created, particularly annoying for outside of the Method collecting the query results data, when we MUST have intellisense.

So your idea of always making a 'concrete' Class for the relevant property values seems a much better idea to me - even if it is the simplest of Class definitions. Even though we may still use anonymous types within the query syntax for things like the Group clause - the select clause can still use a user define 'concrete' class for the results set.

This is just another of those 'things' that the published eBooks, by well known authors, do not address and/or answer. There are quite a few such issues related to LINQ, that's why I ended up writing and creating my own 'ClickStartLINQ' eNotes.

I will do some more testing on this as I fill-out my sections and sub-sections in my Appendix 'X' in the eNotes I am compiling / writing.

Regards,
Phil.

Please Log in or Create an account to join the conversation.

  • Page:
  • 1