easy question about linq

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

I just wonder if this linq query
int totalRows = ccrFromObj.myWorksheetRowList.Count(o => o.WorksheetID ==
workSheetID);

can be write in this way just adding a return for the expression
int totalRows = ccrFromObj.myWorksheetRowList.Count(o => return
o.WorksheetID == workSheetID);

//Tony
 
Tony said:
Hello!

I just wonder if this linq query
int totalRows = ccrFromObj.myWorksheetRowList.Count(o => o.WorksheetID ==
workSheetID);

can be write in this way just adding a return for the expression
int totalRows = ccrFromObj.myWorksheetRowList.Count(o => return
o.WorksheetID == workSheetID);

//Tony

No, that doesn't make sense. The result of the comparison is already
returned to the Count method, you can't add an extra return there.

What is it that you are trying to accomplish with that?
 
Hello!

I know that an implicit return is used but I just thought it make more
understandable if I could
use an explicit return

//Tony
 
Tony said:
Hello!

I know that an implicit return is used but I just thought it make more
understandable if I could
use an explicit return

//Tony

The lambda expression will compile into a delegate, so you can use that
form instead:

int totalRows = ccrFromObj.myWorksheetRowList.Count(
delegate(Worksheet o) {
return o.WorksheetID == workSheetID;
}
);
 
Tony Johansson said:
Hello!

I just wonder if this linq query
int totalRows = ccrFromObj.myWorksheetRowList.Count(o => o.WorksheetID ==
workSheetID);

can be write in this way just adding a return for the expression
int totalRows = ccrFromObj.myWorksheetRowList.Count(o => return
o.WorksheetID == workSheetID);


You need to add braces:-

int totalRows = ccrFromObj.myWorksheetRowList.Count(o => {return
o.WorksheetID == workSheetID;});

However I think that would be confusing. I prefer to use delegate when
creating an anonymous function where a return would be used and leave lamdas
purely for expressions and hence no return (or braces) are necessary.
 
Back
Top