0

FIDDLE

Utility Function:

  public static string GetProperties<T>(string alias="")
    {
       if (alias.Length>0)
       {
          return typeof(T).GetProperties().Select(x => x.Name).Aggregate((x, y) => 
            alias + " = " + alias + "." + x + "," + Environment.NewLine + alias + " = " + alias + "." + y + ",");
       }
       else
       {
           return typeof(T).GetProperties().Select(x => x.Name).Aggregate((x, y) => x + Environment.NewLine + y);
       }
    }

Code:

    public class ContainerInLog
    {

        public int ContainerInLogID { get; set; }
        public int ContainerInID { get; set; }
        public int CargoID { get; set; }      
        public int LoadStatus { get; set; }    

    }

    public static void Main()
    {
         string list = GetProperties<ContainerInLog>("y");
        Console.WriteLine(list);
    }

Result:

y = y.y = y.y = y.ContainerInLogID,
y = y.ContainerInID,,
y = y.CargoID,,
y = y.LoadStatus,

Expected Result:

ContainerInLogID = y.ContainerInLogID,
ContainerInID = y.ContainerInID,
CargoID = y.CargoID,
LoadStatus = y.LoadStatus,
3
  • That fiddle is not what you have posted here... Commented May 16, 2019 at 11:23
  • @DavidG apologies, please check now. Commented May 16, 2019 at 11:23
  • Don't use Aggregate with strings. You generate a lot of intermediate strings which are then thrown away, making it expensive. Use string.Join / a StringBuilder as appropriate. Commented May 16, 2019 at 11:29

2 Answers 2

5

If you are really stuck on returning the entire concatenated string instead of returning an enumerable of them, I wouldn't use Aggregate here, just use string.Join. Also, you can simplify the statement by crafting the string inside the Select. For example:

return string.Join(
    Environment.NewLine,
    typeof(T)
        .GetProperties()
        .Select(x => $"{x.Name} = {alias}.{x.Name},"));

Bonus: If you change the separator to $",{Environment.NewLine}" you can remove the inline comma and you won't get the final comma on the end of your string (example fiddle).

Sign up to request clarification or add additional context in comments.

6 Comments

That is working just fine. But could you tell me what was causing the initial y.y = y.y
Well what changed? What is different? Are you using the latest version of C#?
nothing that I can think of. I am using vs2013, .net framework 4.5.1
That means you're not using a modern version of the C# compiler, which means the $"...." notation doesn't work. Replace those with string.Format.
that's strange, because it worked the first few times.
|
1

@DavidG has the nicer solution here, but whats going wrong with your aggregation is the following:

.Aggregate((x, y) => 
            alias + " = " + alias + "." + x + "," + Environment.NewLine + alias + " = " + alias + "." + y + ",");

The Aggregate selector function (your (x,y)) takes the following form:

Func<TAccumulate,TResult> resultSelector

That means in your case, x is the accumulated aggregate result already, eg "ContainerInLogID = y.ContainerInLogID".

But to make the next aggregate, you transform x again: alias + " = " + alias + "." + x, making "y = y.y = y.ContainerInLogID". And so on for each following property, each one adding another prefix of "y = y.".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.