1

Just two quick questions

1.What is the below statement called?

Func<usersDto, bool> predicate

2.How is it different from below?

Expression<Func<usersDto, bool>>

3.How do i convert Func<type1,bool> to Func<type2,bool>. Seems like advanced stuff me

Example

GetUsers(Func<UserDto,bool> predicate)
{    
  return EfContext.Users.Where ( convert above predicate to be passed here)
                        .Cast<>();       
}
horgh
  • 17,918
  • 22
  • 68
  • 123
Deeptechtons
  • 10,945
  • 27
  • 96
  • 178
  • question might have been answered already. [http://stackoverflow.com/questions/793571/why-would-you-use-expressionfunct-rather-than-funct][1] [1]: http://stackoverflow.com/questions/793571/why-would-you-use-expressionfunct-rather-than-funct – mdcuesta Dec 28 '12 at 06:15
  • 5
    The first one is called predicate. The second one does not have a name, but I'm gonna call it Al. – SWeko Dec 28 '12 at 06:16
  • @mdcuesta #2 answered from your link, #1 and #3 remains open – Deeptechtons Dec 28 '12 at 06:16
  • 1
    It's three questions, actually. You might want to split them into separate questions to better fit the Q&A format. – Dennis Traub Dec 28 '12 at 06:18
  • 2
    Oh, and statement #1 is called *variable declararation* – Dennis Traub Dec 28 '12 at 06:20
  • @DennisTraub `split them into separate questions` I would have been downvoted to oblivion that i cannot google for answers – Deeptechtons Dec 28 '12 at 06:21
  • 1
    Can you give an example of how #3 would be used? – SWeko Dec 28 '12 at 06:22
  • Please give an example of a specific problem you're trying to solve. The questions you've asked are virtually impossible to answer without some context. – phoog Dec 28 '12 at 06:24
  • @SWeko addded an example, basically the GetUsers method lies on Interface that will be called inside asp.net mvc controllers. Implementation will execute the predicate against Entity Framework Context which has another type `Users`. I want to run the predicate that Interface gets to the entity framework context entities – Deeptechtons Dec 28 '12 at 06:30
  • Couldn't your `GetUsers` method take a `Func` parameter instead of `Func`? – phoog Dec 28 '12 at 06:40
  • @phoog But then i will be executing against a User Entity. No separation of concerns, i would be hard to track down from where the UserEntity is modified – Deeptechtons Dec 28 '12 at 06:44
  • Possible duplicate (in more than one ways): http://stackoverflow.com/questions/14065401/linq-expression-type-conversion – Dennis Traub Dec 28 '12 at 06:46
  • @Deeptechtons in that case, and assuming that the User entity and the userDto have a common interface, you could probably take an `Expression>`, and create an expression visitor that constructs an equivalent `Expression>` that you can pass to the EF context. Danny Chen's link explains that. – phoog Dec 28 '12 at 06:48
  • @DennisTraub huh? How is it useful to say that a question is a duplicate of itself? – phoog Dec 28 '12 at 06:50
  • @phoog wrong link, I meant this one http://stackoverflow.com/questions/14065671/linq-expression-conversion-type – Dennis Traub Dec 28 '12 at 06:59

1 Answers1

3

A Func<T, TResult> is a built-in delegate that takes a parameter whose type is T and returns a value whose type is TResult. In your question, predicate is a delegate takes an instance of usersDto and returns a bool.

An Expression<Func<T, TResult>> is a uncompiled Func<T, TResult>, it can be analyzed, or packed into another expression as a part.

Q3: see my answer for this question.

Community
  • 1
  • 1
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174