Using output of class where output type is Dynamic.

The screenshot shows that the program generated an error message:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.Collections.Generic.List<FSL.DeveloperTest.Console.ClientData>' does not contain a definition for 'OldClientRef''

However the screenshot also shows that ClientData does have on OldClientRef property.  

Why the error message?  Below is the method called by the line of code immediately before the error message and the ClientData class.

       static dynamic GetDataFromSource(string database, string server, int clientRef,string TableNames)
            string EmptyString = "";
            using (IDbConnection connection = new SqlConnection(ConnectToDatabase(database, server).ConnectionString))
                var parameters = new DynamicParameters();
                parameters.Add("@clientRef", clientRef, DbType.Int32);
                switch (TableNames)
                    case "ClientAccountAndAccount":
                        var data1 = connection.Query<AccountData>("select a.* from ClientAccount c join Account a on a.AccountRef = c.AccountRef where c.ClientRef = @clientRef",
                        parameters, commandType: CommandType.Text);
                        return data1;
                    case "Account":
                        var data2 = connection.Query<ClientAccountData>("select c.* from ClientAccount c where c.ClientRef = @clientRef",
                        parameters, commandType: CommandType.Text);
                        return data2;
                    case "Client":
                        var data3 = connection.Query<ClientData>("select top 1 * from Client where ClientRef = @clientRef", parameters, commandType: CommandType.Text);//Use top 1 instead of data.GetFirstOrDefault
                        return data3;
                        return EmptyString;


Open in new window

using Dapper.Contrib.Extensions;
using System;
using System.Collections.Generic;
using System.Text;

namespace FSL.DeveloperTest.Console
    public class ClientData
        public int ClientRef { get; set; }
        public int OldClientRef { get; set; }
        public string ClientId { get; set; }
        public string ClientName { get; set; }
        public string SortName { get; set; }
        public DateTime? CalculationDate { get; set; }
        public int CompanyRef { get; set; }

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You need to use -
dynamic clientData = GetDataFromSource(fromDatabase, fromServer, clientRef, "Client");

Open in new window

Actually, I retract that, you need to specify the index since it is a List/Array; e.g. -
clientData[0].OldClientRef = clientRef

Open in new window

While ClientData contains a property called OldClientRef, System.Collections.Generic.List does not.  That is what this exception is saying.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlHal2Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.