locked
ASP DOT Net Group BY Linq Query Show View Page Problem RRS feed

  • Question

  • User1896377498 posted

    Hello Sir, I want to do a report i do a linq query after that when i want to run that query work in controller perfectly loop to data featch and after that when want to show that loop in view page that time i show just error and dibugger to see that error not featch data

    // this is successfully working

                var WayBillReport = from TripDeta in _context.TripDetail
                                    join Trip in _context.Trip
                                    on TripDeta.TripNo equals Trip.TripNo
                                    join TouchPoint in _context.TouchPoint
                                    on TripDeta.TouchPointID equals TouchPoint.TouchPointID
                                    join MainRoutes in _context.MainRoute
                                    on Trip.SingleMainRouteId equals MainRoutes.MainRouteID
                                    join RouteType in _context.RouteType
                                    on Trip.SingleTripType equals Convert.ToString(RouteType.SL)
                                    where Trip.DriverId == DriverHRID && Trip.TripDate == Convert.ToDateTime(StartDate) && TripDeta.SingleRoundTrip == "Starting"
                                    select new WayBillVM
                                    {
                                        TouchPoint = TouchPoint.TouchPoint,
                                        mainRoute = MainRoutes.RouteName,
                                        VehicleNumber = Trip.VehicleNo,
                                        TripDate = Trip.TripDate,
                                        TripNumber = Trip.TripNo
                                    };
    
                var ToLIST = WayBillReport.ToList();

    after that when I do that, that is also working successfully.

                var GroupByMainRoute = ToLIST.GroupBy(x => new { x.mainRoute, x.TripNumber }, (key, group) => new
                {
                    Key1 = key.mainRoute,
                    Key2 = key.TripNumber,
                    Result = group.ToList(),
                });
    
    
    
                var groupedUsers = GroupByMainRoute.ToList();

    //this viewbag use grop by data passing to view page
     ViewBag.groupedUsers = groupedUsers;

    but when i loop in that type of in controller to see data get success or not to the debugger that is also ok

                foreach (var group in groupedUsers)
                {
                    var KeyNew = group.Key1;
                    var KeyNew2 = group.Key2;
                    var Re = group.Result;
    
                    foreach (var entry in Re)
                    {
                        var New2 = entry.TouchPoint;
                    }
                }

    but when i do that loop in view page when error 

        foreach (var group in ViewBag.groupedUsers)
        {
            var KeyNew = group.key;
            var KeyNew2 = group.Key2;
            var Re = group.Result;
    
            foreach (var entry in Re)
            {
               var New2 = entry.TouchPoint;
    
            }
    
        }

    How To Solve That Please help me.

    Tuesday, April 20, 2021 7:23 AM

Answers

  • User1686398519 posted

    Hi itrabbi24, 

    The reason is that the anonymous class is internal to the assembly. Keyword dynamic doesn't allow you to bypass member visibility.

    Here is a solution, you can refer to it:

    @foreach (var group in ViewBag.groupedUsers)
    {
        var KeyNew = group.GetType().GetProperty("Key1").GetValue(group, null);
        ... ...
    }

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 21, 2021 2:27 AM