easy question about linq

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
 
G

Göran Andersson

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?
 
T

Tony Johansson

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
 
G

Göran Andersson

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;
}
);
 
A

Anthony Jones

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top