1

I have an issue that's eating my head for the past couple of days. I am unable to find a solution.

I am trying to find the occurrence of '\' in a string using lambda expressions. Here's the code:

Microsoft.Office.Interop.Excel.Range labelSupportTopic = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[resultRange.Row, 29];
int count = labelSupportTopic.Text.ToString().Count(c => c == '\\'); 

I am getting the date from an Excel sheet, therefore the Interop reference.

What's bothering me is that this code is copied and pasted from an exact same VS project whose most of the code I lost somehow. I was lucky to have some part and this was one of it. Regardless, on my other VS project (the one whose code I lost), I am able to Build and it looks fine. But my current project, the one I built from scratch, it throws an error on Build. This entire section, the one that contains this logic is lifted off from my old project (which Builds fine). The error reads "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type" I searched the internet but I was unable to find a solution.

Can you help, please?!

4
  • If you run your code in debug mode, can you see if Microsoft.Office.Interop.Excel.Range is dynamic? If it is, that's your problem. You can't use lambdas or extension methods with dynamic. Commented Aug 27, 2017 at 23:49
  • ...ToString().Count((Predicate<char>)(c => c == '\\')); Commented Aug 27, 2017 at 23:52
  • @Connor Thanks for the reply. The marked answer fixed it! Commented Aug 28, 2017 at 7:18
  • @JakubDąbek Your response seemed to have fixed the issue with Build (it Build without errors) but it still wouldn't count the number of \ in the string. Thanks for looking in though. Commented Aug 28, 2017 at 7:19

1 Answer 1

1
int count = (labelSupportTopic.Text as string).Count('\\'.Equals);

Because the type of .Text is dynamic, the type of .ToString() is also dynamic, and the type of c in the lambda expression can't be inferred.

In earlier versions of C# that don't have the dynamic keyword (before VS 2010), the type of .Text is object and your original version should compile without issues.

Sign up to request clarification or add additional context in comments.

5 Comments

I wouldn't use as since it implies that you expect it might not be a string, and it would just result in a null reference exception if the text property happens not to be a string. Either use a regular cast if you expect it always to be a string and it would be an error not to be a string, or else use the null coalescing operator to default it to "" in cases where it's not a string. Otherwise I think this answer is correct.
@Sahuagin you are right, but I find this form a bit more readable than ((string)(labelSupportTopic.Text)). .Text can return null for a Range with different text values, but for a single cell it results in at least "", so I removed the ?? "" from my initial sample.
That seemed to have fixed the issue. What I don't understand it, despite having the exact code copied and pasted from another project, it wouldn't work. Any idea what it'd do that? Honestly, I didn't change a thing.
@AnonymousPerson as I mentioned, your original version would work in older versions where .Text returns object instead of dynamic. There must be some small difference between the two projects settings.
@Slai There was hardly a gap of an hour, so I don't suppose version is the reason. Besides, I did not update VS at all. But regardless, thanks for helping. The product is fully functional now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.