c# - How do I make this Func work with Linq to SQL? -


i have abstract, generic repository class encapsulates lot of logic, , able because knows how isolate unique data row, keycompare property, injected repository's constructor:

func<t, t, bool> keycompare { get; } 

the role of property tell base class fields use fetch unique record - it's "id" , keycompare can assigned (passed constructor):

(item1, item2) => item1.id == item2.id 

however gives me flexibility of using linq sql classes have keys span several columns, this:

(item1, item2) => item1.field1 == item2.field1 && item1.field2 == item2.field2 

and works in unit tests, because i'm testing mock repo uses linq object.

production code uses linq sql , since i've been told keycompare property won't behave expected (because func won't translated sql), should using expression instead. started googling around (and apparently func won't translated sql), , saw many examples, nothing i'm doing (is sign of something?).

the way i'm using func follows:

public t item(t item) {     return (_items.singleordefault(e => keycompare(e, item)); } 

i told implement method (with keycompare being expression):

public t item(t item) {     return (_items.singleordefault(keycompare); } 

but won't compile (.singleordefault wants func, not expression) and, if did, wouldn't using item value need give in order intend.

i'm injecting func repo's constructor ninject; here's how bind keycompare constructor argument:

.withconstructorargument("keycompare", (func<mockentity, mockentity, bool>)((item1, item2) => item1.id == item2.id)); 

so i'm stuck. how make work linq sql?

the alternative, if drop keycompare property in base class, write redundant code every time need implement concrete repository, i'd avoid. keycompare property, need write code that's specific each repository type implement new one, , that.

this post has complete [original - has changed since] code abstract repository class i'm talking about: can ninject use anonymous delegate (func) constructorargument?

edit

linq-to-sql : convert func<t, t, bool> expression<func<t, t, bool>> has interesting answer , pinpoint accurate title (and wonder how have missed 1 in research). answers question excellent, detailed answer i'm going try in moment.

the usage little weird, since curried lambda. instead of passing (x,y) => ... passing x => y => ....

i'm sure solution works (it's quite clever), but not explain why passing (x,y) needs backflip , triple axel perform. what's deal 2 inputs? if linq objects can pull off, that's preventing linq sql doing same?

you can covert func expression by

expression<func<t, t, bool>> keycompare 

whether fixes issue depends on in function. should translatable sql. 2 t arguments make me doubt this. somewhere there should where statement accepts expression of <t, bool>.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -