Anonymous Types - syntax needed

This forum is meant for questions and discussions about the X# language and tools
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Anonymous Types - syntax needed

Post by Phil Hepburn »

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
FFhelpSyntax_01.jpg
FFhelpSyntax_01.jpg (22 KiB) Viewed 784 times
FFhelpSyntax_02.jpg
FFhelpSyntax_02.jpg (29.23 KiB) Viewed 784 times
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Anonymous Types - syntax needed

Post by robert »

Phil,

Our syntax for an anonymous type is:

Code: Select all

VAR o := CLASS { Name := "test", Value := "something" }
and in combination with LINQ:

Code: Select all

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
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Anonymous Types - syntax needed

Post by Phil Hepburn »

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.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Anonymous Types - syntax needed

Post by Phil Hepburn »

Robert,

Just a quick post to celebrate my success with your suggested syntax ;-0) Most appreciated.
FFhelpSyntax_05.jpg
FFhelpSyntax_05.jpg (4.19 KiB) Viewed 784 times
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
FFhelpSyntax_06.jpg
FFhelpSyntax_06.jpg (15.01 KiB) Viewed 784 times
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Anonymous Types - syntax needed

Post by Phil Hepburn »

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
AnonTypes_18.jpg
AnonTypes_18.jpg (57.05 KiB) Viewed 784 times
AnonTypes_19.jpg
AnonTypes_19.jpg (16.59 KiB) Viewed 784 times
AnonTypes_17.jpg
AnonTypes_17.jpg (29.68 KiB) Viewed 784 times
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Anonymous Types - syntax needed

Post by robert »

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.

Code: Select all

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
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Anonymous Types - syntax needed

Post by Phil Hepburn »

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.
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Anonymous Types - syntax needed

Post by robert »

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
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Anonymous Types - syntax needed

Post by Phil Hepburn »

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.
XSteamAT_01.jpg
XSteamAT_01.jpg (51.28 KiB) Viewed 785 times
XSteamAT_02.jpg
XSteamAT_02.jpg (53.85 KiB) Viewed 785 times
XSteamAT_03.jpg
XSteamAT_03.jpg (57.62 KiB) Viewed 785 times
XSteamAT_04.jpg
XSteamAT_04.jpg (24.44 KiB) Viewed 785 times
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Anonymous Types - syntax needed

Post by robert »

Phil,
I am not sure if you what you want is even possible in C#. Try the following example:

Code: Select all

FUNCTION Start AS VOID
	
	VAR o := CLASS{FirstName := "Phil", LastName := "Hepburn"}
	? o           
	? o:GetType()
	Console.Read()
The output is:

Code: Select all

{ 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)

Code: Select all

	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
robert@xsharp.eu
Post Reply