none
using ..select() and where() with projection in EF 3.5 and dynamic string RRS feed

  • General discussion

  • it seems best to

    - use .where before .select as this order affords the flexibility of using criteria column that is not part for the projection,

    - one can use dummy System.Data.Objects.ObjectParameter as argument to the 2nd parameter of .select when both .where and .select are used

    I was working on some project component that need to change dynamically  per user request.

    I wasted time searching for articles examples and I failed to find what I want for the equivalent.  hope this will help someone and ossibly get some expert to help us improve

    var myValuationDetl = from d in ,yContext.Daily_Holdg_detl
                where d.valuatation_dt = dt && d.Acct_grp_id = acctGrpId && d.subAcct_ID=subAcctID
                select new
                {
                 Qty = d.Qty,
                 Lst_Qot = d.Lst_Qot,
                 Cost = d.Cost,
                 Mket_Value=d.Mket_Value,
                 Adj_Flag=d.Adj_Flag,
                 valuatation_Dt = d.Valuation_dt
                };

    after hacking with different combination, I was able to get somethinng like this working:

    string sProjn ="it.Valuation_dt, it.Qty, it.Lst_Qot, it.Cost, it.Mkt_Value, it.Adj_Flg";
    // actually the sProjn value is dervied from user input, but I just show a sample value. same goes for the criteria string
    string sCriteria = "it.valuation_dt=@ValuationDt and it.Acct_grp_id=@acctGrpId and it.subAcct_ID=@subAcctID";
    System.Data.Objects.ObjectParameter[] parms = new System.Data.Objects.ObjectParameter[3];
       parms[0] = new System.Data.Objects.ObjectParameter("ValuationDt", dt);//"Valuation_dt",dt);ValuationDt
       parms[1] = new System.Data.Objects.ObjectParameter("AcctGrpID", acctGrpId); //"Acct_grp_ID", acctGrpId);AcctGrpID
       parms[2] = new System.Data.Objects.ObjectParameter("subAcctID", subAcctID);
    var mydbrec = myContext.Holdg.Where(sCriteria, parms)
         .Select(sProjn, new System.Data.Objects.ObjectParameter[0]);

     now mydbrec can be used as datasource for some datagridview or whatever

    however when mydbrec is used datasource for DGV, one need work work on teh dgv as the result is that everything is be default left alligned (awfull for numerical columns) and columns are even sized



    • Edited by fs - ab Friday, April 20, 2012 3:55 AM
    Friday, April 20, 2012 3:42 AM

All replies

  • EF3.5, more and more people use EF4.0+ version now.

    Fighting like Allen Iverson. Neve give up!

    Sunday, April 22, 2012 11:41 AM
  • unfortunately we are workign a project with visaul stdudio 2008 standard. upgarding teh project to 2010 to ef 4 is out the question for quite a while
    Wednesday, April 25, 2012 6:30 AM