5

I have these two classes

public class BuyEstimateResult
{
    public string SubTitle { get; set; }
    public string Value { get; set; }
    public string Formulae { get; set; }
}
public class SellerReportClass
{
    public string Entityname { get; set; }
    public double EntityAmt { get; set; }
    public string Formulae { get; set; }
}

I have to make them such that it should be converted as

public class KeyValue
{
    public string key {get;set;}
    public string value {get;set;}
}

if I pass BuyEstimateResult, its SubTitle should be key and Value should be Value of KeyValue Class and if I pass SellerReportClass then Entityname sould be key and EntityAmt should be Value

any ideas how can it be done

NOTE: I will get List of Both the class

Jim G.
  • 15,141
  • 22
  • 103
  • 166
1Mayur
  • 3,419
  • 5
  • 40
  • 64
  • which method you want to pass list of both objects? pls could you post the code? – cuongle Oct 23 '12 at 08:46
  • Does this answer your question? [How to convert object to Dictionary in C#?](https://stackoverflow.com/questions/11576886/how-to-convert-object-to-dictionarytkey-tvalue-in-c) – Jim G. Apr 28 '22 at 17:00

1 Answers1

10

You can use the implicit operator in C# to convert it into a KeyValue like you want - http://msdn.microsoft.com/en-us/library/z5z9kes2(v=vs.110).aspx

public static implicit operator KeyValue(BuyEstimateResult ber)
{
     return new KeyValue { Key = ber.SubTitle, Value = ber.Value }
}

Edit

To be more specific on how to implement this:

public class BuyEstimateResult
{
    public string SubTitle { get; set; }
    public string Value { get; set; }
    public string Formulae { get; set; }

    public static implicit operator KeyValue(BuyEstimateResult ber)
    {
        return new KeyValue {Key = ber.SubTitle, Value = ber.Value};
    }
}

public class SellerReportClass
{
    public string Entityname { get; set; }
    public double EntityAmt { get; set; }
    public string Formulae { get; set; }

    public static implicit operator KeyValue(SellerReportClass sell)
    {
        return new KeyValue { Key = sell.Entityname, Value = sell.EntityAmt.ToString(CultureInfo.InvariantCulture)};
    }
}

public class KeyValue
{
    public string Key { get; set; }
    public string Value { get; set; }
}

public class Program
{
    static void Main()
    {
        var listB = new List<BuyEstimateResult>
                        {
                            new BuyEstimateResult {SubTitle = "BER1", Value = "BER1_VALUE"},
                            new BuyEstimateResult {SubTitle = "BER2", Value = "BER2_VALUE"}
                        };

        var listS = new List<SellerReportClass>
                        {
                            new SellerReportClass {Entityname = "SELL1", EntityAmt = 1.0},
                            new SellerReportClass {Entityname = "SELL2", EntityAmt = 2.5}
                        };

        foreach (KeyValue kv in listB)
            Console.WriteLine(kv.Key + ":" + kv.Value);

        foreach (KeyValue kv in listS)
            Console.WriteLine(kv.Key + ":" + kv.Value);
    }
}

To get a single list of KeyValue objects from the two different lists you can do something like this:

var KeyValueList = listB.ConvertAll(i => (KeyValue) i);
KeyValueList.AddRange(listS.ConvertAll(i => (KeyValue) i));
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
manojlds
  • 290,304
  • 63
  • 469
  • 417