Link to home
Start Free TrialLog in
Avatar of Kristen Jones
Kristen JonesFlag for United States of America

asked on

C# LINQ Syntax help with Where clause

I have this code

public ActionResult GRIDPRINTS_Read(int woid, [DataSourceRequest]DataSourceRequest request)
        {
            IQueryable<GRIDPRINTS> gridprints = db.GRIDPRINTS;
            DataSourceResult result = gridprints.ToDataSourceResult(request, c => new GRIDPRINTS
            {
                WOID = c.WOID,
                GRIDPRTID = c.GRIDPRTID,
                GRIDID = c.GRIDID,
                MAPNAME = c.MAPNAME,
                SCALE = c.SCALE,
                PAGENUMBER = c.PAGENUMBER,
                ERRORS = c.ERRORS,
                STATUS = c.STATUS,
                NOTIFICATION = c.NOTIFICATION
            });

            return Json(result, JsonRequestBehavior.AllowGet);
        }

Open in new window


IT works fine Except I want to pass the woid to a where clause in the results.  So I added this line

.where(c.WOID==woid)    

public ActionResult GRIDPRINTS_Read(int woid, [DataSourceRequest]DataSourceRequest request)
        {
            IQueryable<GRIDPRINTS> gridprints = db.GRIDPRINTS;
            DataSourceResult result = gridprints.ToDataSourceResult(request, c => new GRIDPRINTS
            {
                WOID = c.WOID,
                GRIDPRTID = c.GRIDPRTID,
                GRIDID = c.GRIDID,
                MAPNAME = c.MAPNAME,
                SCALE = c.SCALE,
                PAGENUMBER = c.PAGENUMBER,
                ERRORS = c.ERRORS,
                STATUS = c.STATUS,
                NOTIFICATION = c.NOTIFICATION

             .where(c.WOID==woid)   

            });

            return Json(result, JsonRequestBehavior.AllowGet);
        }

Open in new window


It is failing with unknown method "where of string"  I am sure it is something simple stupid but after an hour of trying everything I am turning to the experts.
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

Shooting from the hip here... but must the .where not be just before the semicolon?
}).where(c.WOID==woid);

Open in new window

Avatar of Kristen Jones

ASKER

I thought that too but it errors on the  
c. 

Open in new window


Unknown entity which I think makes sense, since it is out of the ToDatasourceResult(c=>...)
It errors because you are performing a new task, you have to resupply an identifier for the anonymous object; e.g. -
}).Where(c => c.WOID==woid);

Open in new window

Complete code -
public ActionResult GRIDPRINTS_Read(int woid, [DataSourceRequest]DataSourceRequest request)
{
    IQueryable<GRIDPRINTS> gridprints = db.GRIDPRINTS;
    DataSourceResult result = gridprints.ToDataSourceResult(request, c => new GRIDPRINTS
    {
        WOID = c.WOID,
        GRIDPRTID = c.GRIDPRTID,
        GRIDID = c.GRIDID,
        MAPNAME = c.MAPNAME,
        SCALE = c.SCALE,
        PAGENUMBER = c.PAGENUMBER,
        ERRORS = c.ERRORS,
        STATUS = c.STATUS,
        NOTIFICATION = c.NOTIFICATION
    }).Where(c => c.WOID == woid);

    return Json(result, JsonRequestBehavior.AllowGet);
}

Open in new window


-saige-
I tried that too, but gives 3 errors:

Error      11      Instance argument: cannot convert from 'Kendo.Mvc.UI.DataSourceResult' to 'System.Linq.IQueryable'       

Error      12      'Kendo.Mvc.UI.DataSourceResult' does not contain a definition for 'Where' and the best extension method overload 'Kendo.Mvc.Extensions.QueryableExtensions.Where(System.Linq.IQueryable, System.Linq.Expressions.Expression)' has some invalid arguments       

Error      13      Argument 2: cannot convert from 'lambda expression' to 'System.Linq.Expressions.Expression'
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah!   perfect  thanks so much!