views:

113

answers:

2

I have an Query expression that uses a predicate type and lambda expression. I am getting desired result with this. But I am not clear with how this expression is getting evaluated.

I tried to break this lambda expression by creating delegate and replacing condition under Where with delegate type.

If I have to rewrite the same thing with writing a delegate instead of anonymous type. What will be the syntax. How the delegate will be return for the same.

if (((DataTable)dgvAssignedRpm.DataSource).AsEnumerable()
    .Where(row => row.Field<long>("FK_RPM_BTN_SETTING_ID") == objRpmButtonHolder.RpmSettingId).Count() > 1)
{
  List<DataRow> listPkgBtnSettings = SearchForExistingSettingId();
}
+1  A: 

I assume the correct delegate replacement would be:

        if (((DataTable)dgvAssignedRpm.DataSource).AsEnumerable().Where(
            delegate(DataRow row) {
                return (row.Field<long>("FK_RPM_BTN_SETTING_ID") == objRpmButtonHolder.RpmSettingId.Count() > 1); 
            }))
        {
            List<DataRow> listPkgBtnSettings = SearchForExistingSettingId();
        }

But it's morning for me, so forgive me if I'm a bit off.

What the where desires is to give a DataRow as a parameter and a bool to return. You could just about fill in anything in the lambda or delegate, as long as it matches these requests.

To your question why it requests Func<> and how it works. The statement you're using is LINQ, so I found you a reference regarding this which can probably explain it better than me: http://blogs.msdn.com/b/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx

But yeah, the last type here in the Func<> is what it returns. (However, I can still recommend using the Lambda expression, as it's pretty clean, neat and serves the Func<> best.

(Also, look at what intellisence gives you when you write "new Func<....", it should give you a good idea of what Func wants and can do!)

Hope I was of help.

Waltes
@Waltes: When i looks at the where clause definition, it asks for Function<T,bool>() predicate. So what does it means. Does it means. Last parameter among predicate i.e. <T,bool> represents return type of delegate. Am I right
Shantanu Gupta
Ehhh, I'll edit my answer to explain that a little better.
Waltes
The named function madgnome described also gives you a good example.Func<> just asks for a signature of <(param)*, (out result)>Whereas Func<> natively supports upto 16 parameters and a result type.
Waltes
@Waltes: Can you please provide this 16 parameter list. I am searching for that for last an hour. I know it supports 16 types but not getting list. I saw that only once some long time back. Yes after your Edit. I am usign Lambda every where but only difference is that I know how to implement them but not clear with concepts that's why I am trying to understand them that's why I posted this question.
Shantanu Gupta
Waltes
+1  A: 
void MethodSignature(...) 
{ 
  ...  

  if (((DataTable)dgvAssignedRpm.DataSource).AsEnumerable()
    .Where(RowCondition)
  {
    List<DataRow> listPkgBtnSettings = SearchForExistingSettingId();
  }

  ...
}

// Where want a Func<T,bool> parameter 
// T is the first parameter type (DataRow here)
// bool represents the return value
bool RowCondition(DataRow row)
{
  return row.Field<long>("FK_RPM_BTN_SETTING_ID") == objRpmButtonHolder.RpmSettingId).Count() > 1
}
madgnome
@madgnome: how did you recognize that T is a parameter not a return type or vice versa. Is there some rule that last parameter in <T,....,bool> is always a return type. If I say <T,....,int> what will be return type here. Should it be compulsory be int
Shantanu Gupta
@Shantanu, yes, a function - by definition - always returns a value, and in C# only returns one value, therefore in any `Func<>` definition, the last type is the return type, and all the others are for the arguments. If you have *no* return type, it's not a function, but an action, so you use `Action<>`
Benjol