Click here to Skip to main content
16,019,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public List<user> GetList(String MatchTypeName)
        {
                var result = new List<dynamic>();
                
                using (SqlConnection conn = new SqlConnection(@"Server= server_name; Uid=xyz;pwd=pass; database=db_name"))
                {
                    try
                    {
                                              
                        #region commented
                        conn.Open();

                        SqlCommand dCmd = new SqlCommand("SP_WEBBASEDREPorTS", conn);

                        dCmd.CommandType = CommandType.StoredProcedure;

                        dCmd.Parameters.Add(new SqlParameter("@MatchType", MatchTypeName));

                        dCmd.Parameters.Add(new SqlParameter("@ModeFlag", "Batting Industry Average"));

                        SqlDataAdapter da = new SqlDataAdapter(dCmd);
                        DataSet ds = new DataSet();
                        DataTable table = new DataTable();
                        ds.Clear();
                            
                        da.Fill(table);
                        conn.Close();


                        foreach (DataRow row in table.Rows)
                        {
                            var obj = (IDictionary<string,>)new ExpandoObject();
                            foreach (DataColumn col in table.Columns)
                            {
                                obj.Add(col.ColumnName, row[col.ColumnName]);
                            }
                            result.Add(obj);
                        }
                        return result;
                       
                    }
                    catch
                    { 
                    
                    }
                }
                return result;           
        }

Can any one let me know how to solve this problem.Error is:

cannot implicitly convert type 'system.collections.generic.list<dynamic> to system.collection.generic.list</dynamic>

It is happening while returning the result e.g: return result
Posted
Updated 28-Aug-13 19:56pm
v3

The error is quite straightforward: you define a return type of List<user>, and you try to return List<dynamic>.
Simply create the result the same type as the method returning type and change the code to fill it's fields rather than trying to fill in all possible fields coming from your data source. Why do you think that the system is able to map your dynamic data to a class?
 
Share this answer
 
Comments
Zoltán Zörgő 29-Aug-13 8:02am    
I can only repeat myself. Review your code. Dynamic objects are not for that. I hardly can imagine that the stored procedure is returning fields that really need ExpandoObject.
Ruter11 29-Aug-13 8:49am    
My scenario is something like this that my stored procedure is returning differ column(like name og column and no of column) while I am passing parameter through drop down list ,do you have any solution how can I achieve this
Zoltán Zörgő 29-Aug-13 9:02am    
You can use dictionary as model, but it is not a good approach. Remember, there is no automatic mapping between ExpandoObject and strongly typed class, but you can use reflection to do this mapping. Or use this: http://stackoverflow.com/questions/7778216/automapper-or-similar-allow-mapping-of-dynamic-types#answer-7778398, which would be an overhead in your case, but you can take ideas. An other note: use dynamic instead of expandoobject.
Ruter11 30-Aug-13 0:29am    
Thanks for reply I will try this
I found out the solution if any one want Please refer this

XYZ_MVC.Models.XYZEntities db = new Models.XYZEntities();
List<SelectListItem> selectMatchTypeList = new List<SelectListItem>();


public ActionResult Index(string MatchTypeName)
{

foreach (MatchTypeMaster MatchType in db.MatchTypeMasters)
{
SelectListItem selectMatchTypes = new SelectListItem
{
Text = MatchType.MatchTypeName,
Value = MatchType.MatchTypeID,

};

selectMatchTypeList.Add(selectMatchTypes);
ViewBag.MatchTypeName = selectMatchTypeList;
}

if (string.IsNullOrEmpty(MatchTypeName))
{
MatchTypeName = "All";
var bugedlist = GetList(MatchTypeName);
return View(bugedlist);
}
else
{
var GridFill = GetList(MatchTypeName);
return View(GridFill);
}

return View();

}

private List<idictionary> ConvertToDictionary(DataTable dtObject)
{
var columns = dtObject.Columns.Cast<datacolumn>();

var dictionaryList = dtObject.AsEnumerable()
.Select(dataRow => columns
.Select(column =>
new { Column = column.ColumnName, Value = dataRow[column] })
.ToDictionary(data => data.Column, data => data.Value)).ToList().ToArray();

return dictionaryList.ToList<idictionary>();
}

public List<idictionary> GetList(String MatchTypeName)
{
DataSet ds = new DataSet();
//var modelList = new List<user>();
// var result = new List<dynamic>();

using (SqlConnection conn = new SqlConnection(@"Server= Server_Name; Uid=sa;pwd=Pass; database=DB_Name"))
{
try
{

#region commented
conn.Open();

SqlCommand dCmd = new SqlCommand("SP_WEBBASEDREPorTS", conn);

dCmd.CommandType = CommandType.StoredProcedure;

dCmd.Parameters.Add(new SqlParameter("@MatchType", MatchTypeName));

dCmd.Parameters.Add(new SqlParameter("@ModeFlag", "All"));

SqlDataAdapter da = new SqlDataAdapter(dCmd);

DataTable table = new DataTable();
ds.Clear();

da.Fill(ds);
conn.Close();

var das = ds.Tables[0].AsEnumerable();

#endregion
}
catch
{

}
}

return ConvertToDictionary(ds.Tables[0]);

}
}

VIEW Code:

@using System.Dynamic
@model List<system.collections.idictionary>
@{
var result = new List<dynamic>();

foreach (var emprow in Model)
{
var row = (IDictionary<string,>)new ExpandoObject();
Dictionary<string,> eachEmpRow = (Dictionary<string,>)emprow;

foreach (KeyValuePair<string,> keyValuePair in eachEmpRow)
{
row.Add(keyValuePair);
}
result.Add(row);
}
var grid = new WebGrid(result);
}

@using (@Html.BeginForm("Index", "ICCRpt", FormMethod.Get))
{

@Html.DropDownList("MatchTypeName", "Select All");
<input type="submit" value="Filter" />
}

@if (@Model != null)
{
@grid.GetHtml(tableStyle: "grid", headerStyle: "head", alternatingRowStyle: "alt");
}
 
Share this answer
 
My Solution Is Check List Name every where whether it has same name..!!!!
 
Share this answer
 
The error message is suggestive enough - you cannot convert one type to another.
Stepping through code will help you get to the exact line of the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900