locked
How do I re use part of a where clause? RRS feed

  • Question

  • User1034446946 posted

    Hi

    I have a repository pattern which is fed in from a service layer, now I have some very complex models.

    In different where clauses starting from different entities, I want to look to see if a navigation property has any records with a fairly complex set of condition, becuase I start from different entities, I want to be able to reuse the same code.

    I can't find what to do via search, any information and advice would be appriciated.

    Saturday, December 19, 2015 2:39 PM

All replies

  • User1686483761 posted

    Hi enedaveyboy,

    In different where clauses starting from different entities, I want to look to see if a navigation property has any records with a fairly complex set of condition, becuase I start from different entities, I want to be able to reuse the same code.

    I'm not sure what you mean. If you'd like to reuse the same code for different entities according to your where condition, you could modify yorur method and extension your method , we could pass the table name and other parameters. I'm not sure whether this is a good way, of course, I also think your idea could be realized. As you say, you could define a navigation property for different entities, we could get the data for different entities.

    Best Regards,

    sudo

    Monday, December 21, 2015 6:13 AM
  • User1034446946 posted

    lets say I have two entities, both have a navigation property to the same entity, I am using a lambda statement   and I want to do a has any on for the same navigation for both entities when I look them up.

    But I am not sure they best way to go about it.

    One option would be to use an iqueryable, and I assumu I could use and extnetion method to return a bool from the iqueryable, but I am not sure it will work and I am also worry about using IQueryable with DI, becuase i heard it can cause issues to the scope of the connection.

    Monday, December 21, 2015 1:13 PM
  • User1686483761 posted

    Hi EnenDaverBoy,

    I want to do a has any on for the same navigation for both entities when I look them up.

    To be honest, i'm still not sure what you want to do. If you want to reuse LINQ query except for where clause, you could refer to the following links.

    http://stackoverflow.com/questions/13212680/reusable-linq-query-except-for-where-clause

    http://stackoverflow.com/questions/4952602/how-to-reuse-where-clauses-in-linq-to-sql-queries

    Best Regards,

    sudo

    Wednesday, December 23, 2015 7:25 AM