community I have a stored procedure that returns this JSON as a response:
[ { "displayorder": 2, "oppItemID": 4, "opportunityName": "Net 10", "title": "New Amazing Promotion", "description": "Amazing", "image": "imagelink", "opDate": "2018-08-09T22:00:00.937", "slideOrder": 0, "status": false, "isCover": false }, { "displayorder": 1, "oppItemID": 2, "opportunityName": "Simple Mobile", "title": "New promo", "description": "Amazing", "image": "imagelink", "opDate": "2018-08-09T22:00:00.937", "slideOrder": 0, "status": false, "isCover": false }, { "displayorder": 1, "oppItemID": 3, "opportunityName": "Simple Mobile", "title": "New Amazing Promotion", "description": "Amazing", "image": "imagelink", "opDate": "2018-08-10T22:00:00.937", "slideOrder": 0, "status": false, "isCover": false }, { "displayorder": 8, "oppItemID": 5, "opportunityName": "Verizon", "title": "New Amazing Promotion", "description": "Amazing", "image": "imagelink", "opDate": "2018-08-09T22:00:00.937", "slideOrder": 0, "status": false, "isCover": false }, { "displayorder": 8, "oppItemID": 27, "opportunityName": "Verizon", "title": "New Amazing", "description": "Amazing", "image": "imagelink", "opDate": "2018-08-22T22:00:00.937", "slideOrder": 0, "status": false, "isCover": false } ] As you can see, there are multiple objects that have the same "opportunityName"
What I want to do is group them by that specific "opportunityName" and have a list of the objects i.e:
"Varizon":{ 0:{ "displayorder": 8, "oppItemID": 5, "title": "New Amazing Promotion", "description": "Amazing", "image": "imagelink", "opDate": "2018-08-09T22:00:00.937", "slideOrder": 0, "status": false, "isCover": false }, 1:{ "displayorder": 8, "oppItemID": 27, "title": "New Amazing", "description": "Amazing", "image": "imagelink", "opDate": "2018-08-22T22:00:00.937", "slideOrder": 0, "status": false, "isCover": false } } And the same for the rest of the opportunity Names
This is the code:
public List<OpportunitiesByNameForDisplay> GetBdmOpportunites(string rswnum) { List<OpportunitiesByNameForDisplay> OpportunitiesByNameForDisplay = new List<OpportunitiesByNameForDisplay>(); SqlParameter[] sqlParams; List<object> real = new List<object>(); try { var rswnumber = new SqlParameter("@AccountNumber", rswnum); sqlParams = new SqlParameter[1] { rswnumber }; string sql = "exec RSW_Reports_API.dbo.sp_CRMApp_GetAppointmentOpportunitiesFromZoomTesting " + rswnumber ; OpportunitiesByNameForDisplay = db.Database.SqlQuery<OpportunitiesByNameForDisplay>(sql,sqlParams).ToList(); real = OpportunitiesByNameForDisplay.GroupBy(g => new List<Object> { g.OpportunityName, slide = new { g.Title, g.Description, g.Image, g.opDate, g.status, g.SlideOrder, g.displayorder } }); } catch (Exception ex){} return OpportunitiesByNameForDisplay; } I m getting invalid initializer declarator on g.OpportunityName Is there any way to make this work or better?