locked
All Classes RRS feed

  • Question

  • For example I have model with class Pipes, Ventilators and so on.

     in some metod I need walk try all dbSet included in context.

    for example

    var query = context.Pipes.Where(d => d.VentSystem.VentSystemID == ventSystem.VentSystemID).OrderBy(x => x.X2).Select(d => d.X2).FirstOrDefault();
     query = context.Ventilators.Where(d => d.VentSystem.VentSystemID == ventSystem.VentSystemID).OrderBy(x => x.Xt).Select(d => d.Xt).FirstOrDefault();
     

    but I dont want write every db set

    I want write something like this

    public void Walk<TEntity>() where TEntity:class

    {

    foreach (TEntity item in collection)??? where I Can GET All DBSet Included in my Context
                    {
    var query = context.Set<TEntity >.Where(d => d.VentSystem.VentSystemID == ventSystem.VentSystemID).OrderBy(x => x.X2).Select(d => d.X2).FirstOrDefault();
                    }

    }

    ??? where I Can GET List with All DBSet Included in my Context



    • Edited by Ahlompys Friday, April 14, 2017 7:09 PM
    Friday, April 14, 2017 7:07 PM

All replies

  • Hi Ahlompys,

    Based on your description, I think it is difficult to achieve it. Because the different entity class need different where condition(different lamada expression).  which you need to do much judgement in your loop statement.

    In addition, about How to get all DbSet from DbContext, please refer to:

    http://stackoverflow.com/questions/35207159/how-to-get-all-dbset-from-dbcontext

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 17, 2017 9:03 AM
  • I try but item not suitable to set. VS write item is variable but used like a type 

    how convert in or do something???

    using (var context = new ApplicationDbContext())
                {
                    foreach (var item in context.GetType().GetProperties())
                    {                    
                         var query = context.Set<item>().Where(d => d.VentSystem.VentSystemID == ventSystem.VentSystemID).OrderBy(x => x.X2).Select(d => d.X2).FirstOrDefault();
                    }
                }


    • Edited by Ahlompys Monday, April 17, 2017 11:41 AM
    Monday, April 17, 2017 11:40 AM
  • Hi Ahlompys,

    As I mention before,   it is difficult to achieve it. Because the different entity class need different where condition(different lamada expression and return type). I would suggest that you retrieve the data one by one.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 20, 2017 2:48 AM