none
error cannot implicitly convert type object to RRS feed

  • Question

  • please anyone can help when passing object there is error.

    public List<Sale> Result()
            {
               List<Sale> salesdata = new List<Sale>();
                using (chartdbEntities2 am = new chartdbEntities2())
                {
                    salesdata = am.Sales.ToList();

                }
                var salesdet = new object [salesdata.Count + 1];
                salesdet[0] = new object[]
                                 {
                                     "Month",
                                      "Amount"
                                  };
                int j = 0;
                foreach (var i in salesdata)
                {
                    j++;
                    salesdata[j] = new object[] { i.Month, i.Amount }; -----> error near this line 

                }

                return salesdata;
            }

                       
    • Edited by Priya Arun Monday, November 5, 2018 12:32 PM
    Monday, November 5, 2018 12:31 PM

Answers

  • salesdata[j] = new Sale(params) { Prop1=value1, Prop2=value }
    • Marked as answer by Priya Arun Wednesday, November 7, 2018 5:07 AM
    Monday, November 5, 2018 2:44 PM
  • The error lines says this: assign to salesdata[j] = new object[]. That means that salesdata is an array of object array elements. But if you go back to the definition o fsalesdata it is defined as List<Sale>. You are trying to mix types and that isn't going to work. Your new statement for salesdata[j] needs to be creating Sale objects, not object[] instances.

    Going a little bit further, your newing up of salesdata isn't even needed because you are resetting it to ToList() inside your using statement. The salesdet variable and code doesn't seem needed at all.

    List<Sale> salesData = null;
    using (var am = new chartdbEntities2())
    {
       salesData = am.Sales.ToList();
    };
    
    

    At this point though your code doesn't make any sense. salesData is the data from your DB call. You then enumerate it and wipe out the original Sale objects with a new instance that is using the same Month and Amount from the original. Why would you do this? You already have the Sale item created.

    It almost looks like you meant to use salesdet instead of salesdata in the foreach loop. It looks like maybe you're trying to capture a subset of the Sale information and return that. Is that what you're trying to do?

    //Create a new array with the Month/Amount of the salesdata
    var salesDet = salesdata.Select(i => new { Month = i.Month, Amount = i.Amount }).ToArray();

    But at this point you cannot return this array because it is an anonymous type. If you need to pass data to/from a method then you'll want to create a new type to wrap that. Do not try to use object. That is just making all your code harder to read, write and understand.

    //New type outside of this function
    public class SalesModel
    {
       //Guessing types here
       string Month { get; set; }
       decimal Amount { get; set; }
    }
    
    //Function signature
    public IEnumerable<SaleModel> Result ()
    {
       List<Sale> salesdata = null;
       using (var am = new chartdbEntities2())
       {
          salesdata = am.Sales.ToList();
       };
    
       //Create a new array with the Month/Amount of the    salesdata
       var salesDet = salesdata.Select(i => new SalesModel() { Month = i.Month, Amount = i.Amount }).ToArray();
    }
    I really question why you wouldn't just return Sale though. The model doesn't seem to gain you anything.

    If for some odd reason you really, really don't want to create a simple model type then you can use a tuple instead (either Tuple<T> or the C# tuple).


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, November 5, 2018 4:10 PM
    Moderator
  • thank you so much

    • Marked as answer by Priya Arun Wednesday, November 7, 2018 5:07 AM
    Tuesday, November 6, 2018 5:05 AM

All replies

  • Maybe, you can come up with the rest of the exception message that you have not posted. Can't convert from what to what?
    Monday, November 5, 2018 1:05 PM
  • salesdata[j] = new Sale(params) { Prop1=value1, Prop2=value }
    • Marked as answer by Priya Arun Wednesday, November 7, 2018 5:07 AM
    Monday, November 5, 2018 2:44 PM
  • The error lines says this: assign to salesdata[j] = new object[]. That means that salesdata is an array of object array elements. But if you go back to the definition o fsalesdata it is defined as List<Sale>. You are trying to mix types and that isn't going to work. Your new statement for salesdata[j] needs to be creating Sale objects, not object[] instances.

    Going a little bit further, your newing up of salesdata isn't even needed because you are resetting it to ToList() inside your using statement. The salesdet variable and code doesn't seem needed at all.

    List<Sale> salesData = null;
    using (var am = new chartdbEntities2())
    {
       salesData = am.Sales.ToList();
    };
    
    

    At this point though your code doesn't make any sense. salesData is the data from your DB call. You then enumerate it and wipe out the original Sale objects with a new instance that is using the same Month and Amount from the original. Why would you do this? You already have the Sale item created.

    It almost looks like you meant to use salesdet instead of salesdata in the foreach loop. It looks like maybe you're trying to capture a subset of the Sale information and return that. Is that what you're trying to do?

    //Create a new array with the Month/Amount of the salesdata
    var salesDet = salesdata.Select(i => new { Month = i.Month, Amount = i.Amount }).ToArray();

    But at this point you cannot return this array because it is an anonymous type. If you need to pass data to/from a method then you'll want to create a new type to wrap that. Do not try to use object. That is just making all your code harder to read, write and understand.

    //New type outside of this function
    public class SalesModel
    {
       //Guessing types here
       string Month { get; set; }
       decimal Amount { get; set; }
    }
    
    //Function signature
    public IEnumerable<SaleModel> Result ()
    {
       List<Sale> salesdata = null;
       using (var am = new chartdbEntities2())
       {
          salesdata = am.Sales.ToList();
       };
    
       //Create a new array with the Month/Amount of the    salesdata
       var salesDet = salesdata.Select(i => new SalesModel() { Month = i.Month, Amount = i.Amount }).ToArray();
    }
    I really question why you wouldn't just return Sale though. The model doesn't seem to gain you anything.

    If for some odd reason you really, really don't want to create a simple model type then you can use a tuple instead (either Tuple<T> or the C# tuple).


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, November 5, 2018 4:10 PM
    Moderator
  • thank you so much

    • Marked as answer by Priya Arun Wednesday, November 7, 2018 5:07 AM
    Tuesday, November 6, 2018 5:05 AM
  • Hi Priya Arun,

    If the reply if helpful to you, please mark it as answer, it will beneficial to other communities who have the similar issue.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 6, 2018 6:28 AM
    Moderator