.net - DRY LINQ Predicate Filters for Multiple Tables -


Suppose I have two tables, tables, and tables, each record B has one or more related records in IA. I should use a reusable filter which predicts. I can do something like this (like Lin-to-SQL):

  Private expression & lt; Func & lt; ARERORD, bool & gt; & Gt; Filter aggravated () {return x = & gt; X.Name == "Test"; } Private IQueryable & lt; ARERORD & gt; GetRecords () {DataContext.TableA.Where Return (Filter Implant ()); }  

This works fine, but I want to find TableB, but use the same "filter" on the foreign key to know how I relate to the telephone. To complete the question below. Select

  var query = from b in DataContext.B where b.A.Name == "test" b;  

I'm just thinking that there is a best method to make reusable "ou" sections that will help in many tables.

Edit - To clarify, I'm not looking for a way to apply uniforms for ARecord and BRecord types.

  Private expression & lt; Func & lt; BRecord, bool & gt; & Gt; I'm looking for a way (somehow, not necessarily I was thinking beforehand) with lines; FilterPredicate2 () {return x = & gt; X.A.Name == "Test"; }  

Thanks in advance.

You can do this by defining an interface on A and B

  Public interface IHasName // contrived, I know {string name {get}}}  

The LINQ-to-SQL class is partial, so in your part of the partial class definition, You can add the interface like this:

  Public Partial Category A: IHasName {} Public Partial Category B: IHasName {}  

As you see No, no implementation required It should be done as the name property has been implemented in the portion generated in Linq-to-Sql.

Now the IHasName interface to implement your predictions, and you are all set:

  Private expression & lt; Func & lt; T, bool & gt; & Gt; Filter Pedicate (name of string) where T: IHasName {return x = & gt; X.Name == Name; }  

You still need to be able to define an extension method on the IQueryable:

  Public Static GetByName & lt; T & gt; (This is the IQueryable & lt; T & gt;; query, string name) where T: IHasName {query returns Where (filter (name)). SingleOther Default (); Small warning: Surely, the property in the interface ('name') must match exactly with the attribute name in the implementation classes. Assume that you have a class with the property 'MyName', you can take the examination to implement the IHasName interface like this:  
  public partial class C: IHasName {public string name {MyName;}}}  

This will definitely not work because Linq-to-Sql expression parser will use 'name' instead of the actual property 'mine', so it's valid SQL will not be able to map this expression.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -