tortuga
tortuga

Reputation: 414

Linq to entities - Lambda - Concatenate strings

I would like to concatenate a string using lambda to compare that concatenated value against a certain condition.

Invoices = Invoices.Where(f => ((string)f.invoice_prefix + String.Format("{0:0000}", Convert.ToInt32(f.invoice_number))).ToLower().Equals(condition7));

But I get an error message :

The name 'f' does not exist in the current context

Tried several String.Format and String.Concat variants like

Invoices = Invoices.Where(f => (String.Format("{0}{1}",f.invoice_prefix,String.Format("{0:0000}", Convert.ToInt32(f.invoice_number)))).ToLower().Equals(condition7));

but no success... Can somebody help me with the syntax?

Thanks in advance!

Upvotes: 0

Views: 903

Answers (1)

Nick Butler
Nick Butler

Reputation: 24383

Linq to Entities doesn't understand all of the .NET framework methods.

In order to run this as a SQL statement on the database, you need to only use operators that can be converted to SQL. That means you need to re-write your predicate using primitive data types.

So something like this:

string prefixCondition = ...
int invoiceNumberCondition = ...

Invoices.Where( f =>
  f.invoice_prefix == prefixCondition
  &&
  f.invoice_number == invoiceNumberCondition 
)

I recommend using LinqPad to test with, as it shows you the generated SQL statement.

Upvotes: 2

Related Questions