We help IT Professionals succeed at work.

LINQ Entity Framework - COUNT AND QUERY

SA85
SA85 asked
on
please show me how that can be done.
My edmx file was set and i managed to getquery
i have attached an image with what i want to do - please show me how:)

ENTITY
SERVER.CS

    public class OrderViewRepository : BaseRepository<OrderView>
    {
...
...
        public IQueryable<OrderView> GetSomeResults()
        {
            return GetQuery().Where(x => x.SHIPMENT != null);
        }






CLIENT.ASPX.CS

        protected void Page_Load(object sender, EventArgs e)
        {

            OrderViewRepository rep = new OrderViewRepository();
            IQueryable<OrderView> results= rep.GetSomeResults();

            DataViewer.DataSource=results;
            DataViewer.DataBind();

        }






WEBPAGE.ASPX

<form id="Form1" runat="server">
        <asp:GridView runat="server" ID="DataViewer">
            <Columns>
                <asp:BoundField DataField="SHIPMENT" />
            </Columns>
        </asp:GridView>
    </form>

Open in new window

Comment
Watch Question

Something like this:
return GetQuery().GroupBy (d => d.DRIVER_ID).Select (g => new { Driver = g.Key, Count = g.Count(), CountReceiveName = g.Count (x => x.RECEIVE_NAME != null) });

Open in new window


You then need to bind to DataFields named "Driver", "Count" and "CountReceiveName"

Author

Commented:
what do you mean by bind?

this is the error i get when using your line:
Error      11      Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<COrders.Domain.Business.OrderData>'. An explicit conversion exists (are you missing a cast?
By binding I meant you need to create the colums nad specifiy the fields you want to display in the GridView control in the ASPX page.

As for the error, it is ocurring because you are trying to convert the query result to the list of OrderView objects. What fields does the OrderView contain?

Author

Commented:
can you show me how to bind?
It depends on the name of the fields in your OrderView object, but if they were named as in my first post, it would go like this:

<asp:GridView runat="server" ID="DataViewer">
            <Columns>
                <asp:BoundField DataField="Driver" />
                <asp:BoundField DataField="Count" />
                <asp:BoundField DataField="CountReceiveName" />
            </Columns>
        </asp:GridView>

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.