none
refresh binding without another query RRS feed

  • Question

  • Have 2 queries loaded from same table loaded into separate grids. functionally assigned and unassigned lists.

    button to assign an item edits 1 field and saves. How can I move the affected record from 1 list to the other and update binding without another sql call.

    I tried...

    linqMeter m = (linqMeter)unassignedDataGridView.SelectedRows[0].DataBoundItem;

    UnassignedMeters.ToList<linqMeter>().Remove(m);
    AssignedMeters.ToList<linqMeter>().Add(m);

    but even calling ToList at all requeries the db which make the add/remove unneccesary.

    and even if I let it I still need to call...

    unassignedBindingSource.DataSource = UnassignedMeters.OrderBy(q => q.LOCID);

    to see the change and OrderBy hits the database again.  Why dosn't the binding detect the change on its own?  and Why can't I Resort my already loaded items without asking the database again?

    Maybe LinkToSQL is just too automatic for its own good. How can I prevent excessive queries when I already have all the data I need.

    Basically I would still be using Datasets but the lack of support for Nulls drove me away.

     



    Thursday, June 2, 2011 7:13 PM

Answers

  • instead of saving the direct query result as in my original post...

    IQueryable<linqMeter> AssignedMeters = from m in ctx.linqMeters where m.SiteID == SiteID orderby m.LOCID select m;

    Save it already listed instead...

    List<linqMeter> lAssignedMeters = (from m in ctx.linqMeters where m.SiteID == SiteID orderby m.LOCID select m).ToList<linqMeter>();

    and now I can manipulate the lists without more trips to the database.

     

     


    • Marked as answer by BenMcCaig Friday, June 3, 2011 6:51 PM
    Friday, June 3, 2011 6:51 PM

All replies

  • Hi Ben,

    Welcome!

    I think you can use Detach Entities and here is a kb: http://www.codeproject.com/KB/linq/linq-to-sql-detach.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 3, 2011 3:59 PM
    Moderator
  • instead of saving the direct query result as in my original post...

    IQueryable<linqMeter> AssignedMeters = from m in ctx.linqMeters where m.SiteID == SiteID orderby m.LOCID select m;

    Save it already listed instead...

    List<linqMeter> lAssignedMeters = (from m in ctx.linqMeters where m.SiteID == SiteID orderby m.LOCID select m).ToList<linqMeter>();

    and now I can manipulate the lists without more trips to the database.

     

     


    • Marked as answer by BenMcCaig Friday, June 3, 2011 6:51 PM
    Friday, June 3, 2011 6:51 PM
  • Alen's answer would do the job but requires more effort than just working with the raw list instead of the queryable
    Friday, June 3, 2011 7:03 PM
  • the catch being that List<ofT> wont be automatically sortable

    so I used http://davybrion.com/blog/2007/09/creating-a-sortable-bindinglist/

    using http://nils-gruson.blogspot.com/2007/11/dynamiccomparer-sorting-list-of-objects.html

    resulting in...

    SortableBindingList<linqMeter> AssignedMeters = new SortableBindingList<linqMeter>((from m in ctx.linqMeters where m.SiteID == SiteID orderby m.LOCID select m).ToList<linqMeter>());

    import once and no fuss. Detatching the entities would need me to create a partial class for every table object I have





    Friday, June 3, 2011 7:53 PM