locked
update linq query result RRS feed

  • Question

  • i am fighting with this for 3 days now !

    i am displaying the result of this linq query in a wpf datagrid

    var v = from p in _context.EXAMENS_VALEURs 
    join b in _context.EXAMENS_TYPES_VALEURs on p.EXV_ID_VALEUR_TYPE equals b.ID_EXAMEN_TYPE_VALEUR
    join c in _context.EXAMENS_TYPES_GROUPEs on b.EXTV_ID_GROUPE equals c.ID_EXAMEN_TYPE_GROUPE
    
    
    select new ValeursResult()
    {
      ID = p.ID_EXAMEN_VALEUR,
      Designation = b.EXTV_DESIGNATION,  
      Groupe = c.EXTG_DESIGNATION,
      Valeur = p.EXV_VALEUR
    };
    
    
    ((CollectionViewSource)this.FindResource("ExamensView")).Source = v.ToList();
    

    the ValeursResult type is declared like this

     public class ValeursResult
            {
               public int ID { get; set; }
               public string Groupe { get; set; }
               public string Designation { get; set; }
               public string Valeur { get; set; }
            }

    the datagrid id bound to the collectionviewdource with this

    <DataGrid  x:Name="grd2" AutoGenerateColumns="True" 
         ItemsSource="{Binding Source={StaticResource ExamensView}}" />

    now i can view the result in my datagrid, i can change the values in the cells ,
    but the update is not done on the server with 

    _context.Savechanges()

    i have also tried this line of code just before calling savechanges

    ((ListCollectionView)((CollectionViewSource)this.FindResource("ExamensView")).View).CommitEdit();

    but again the databse table is never updated
     

    thanks for your help

    Tuesday, January 7, 2014 12:58 AM

Answers

  • First if the three tables have relationships defined in the database between them, EXAMENS_VALEURs -> EXAMENS_TYPES_VALEURs -> EXAMENS_TYPES_GROUPEs, then do what you are doing now and then when you are ready to update the database use the ID_EXAMEN_VALEUR in the result set of type ValeursResult to re-query the database for that item and also return the related records from the other two tables update the columns you need to and then do a SaveChanges.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Fred Bao Wednesday, January 15, 2014 8:35 AM
    Wednesday, January 8, 2014 7:15 PM

All replies

  • Hello,

    >> ((CollectionViewSource)this.FindResource("ExamensView")).Source = v.ToList();

    This line code actually is to banding the source with the “v”, so after editing the datagrid, the “v” should be changed.

    >> now i can view the result in my datagrid, I can change the values in the cells ,but the update is not done on the server with _context.Savechanges()

    Since the source is banding with “v” rather than Entity Framework, the data Entity Framework context has not been changed actually. That is why even you call _context.Savechanges(), the update is not done.

    I notice that the “v” is queried out by join so I guess that the ValeursResult type should be a view rather than a table in database. If I am right, unfortunately, EF does not support update a view which is created by more than one table. The update type should be an entity type.

    For how to binding datagrid wpf entity framework, please refer to link below:

    http://msdn.microsoft.com/en-us/data/jj574514.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 7, 2014 10:14 AM
  • okay, 

    so what the right way to do what i am trying to do please ?

    basically, i want to display data from 2 or three related tables and make them editable , in a datagrid/ listbox/... etc

    Tuesday, January 7, 2014 10:38 AM
  • Hi,

    It seems that what you want is a complete WPF implement. Since I am not a WPF experts what I can do is to provide you some useful information:

    Implementing a generic data access layer using Entity Framework:

    http://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework/

    This post is about how we can develop a generic data access layer (DAL) with full CRUD (Create, Read, Update and Delete) support using Entity Framework.

    WPF Entity Framework CRUD operation review:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/110a7d54-fd32-4d6e-9968-f2beaf53b33e/wpf-entity-framework-crud-operation-review?forum=wpf

    This discussion is regarding a WPF application with basic EF CRUD operations.

    Or you can post your question to the WPF forum, there are WPF experts who will help you better:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 8, 2014 9:53 AM
  • Hi 

    Because you are not returning entities that are in the EF model, in your case ValeursResult, that result set is NOT being tracked because it does not represents a table in the database. There for you need to first retrieve the rows of the data tables you wish to modify then modify those values that need to be updated and then save changes on those entities.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, January 8, 2014 3:52 PM
  • ok i can see, so the only way to do that is to retrieve all the entities from the context, and with their corresponding navigation properties, even if i need only some fields from the join tables ?
    Wednesday, January 8, 2014 6:56 PM
  • >ok i can see, so the only way to do that is to retrieve all the entities from the context, and with their corresponding >navigation properties, even if i need only some fields from the join tables ?

    You could do that, or do as  you are, and marshal the changes from your List<ValeursResult> back to the DbContext in code.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Wednesday, January 8, 2014 7:11 PM
  • First if the three tables have relationships defined in the database between them, EXAMENS_VALEURs -> EXAMENS_TYPES_VALEURs -> EXAMENS_TYPES_GROUPEs, then do what you are doing now and then when you are ready to update the database use the ID_EXAMEN_VALEUR in the result set of type ValeursResult to re-query the database for that item and also return the related records from the other two tables update the columns you need to and then do a SaveChanges.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Fred Bao Wednesday, January 15, 2014 8:35 AM
    Wednesday, January 8, 2014 7:15 PM