views:

803

answers:

7

Hi, anyones knows how to do this?

Edit: I am trying to do >=. I correct the Title.

+4  A: 

string1 => string2 is not supported in C# LinqToSql or not. The String class does not override the => operator at all. It only overrides the != and == operators. You can verify this by trying to compile the following method

public static void Example() {
  int val = "foo" => "bar";
}

If you want to compare to Strings in LinqToSql you should be able to use the static String.Compare(string,string) method.

JaredPar
=> isn't the same as >=, I am unsure if he is talking about a method pointer as in a delegate or an actual >=.
Nick Berardi
@Nick, it's a typo in my example. The SO TextEditor compiler is terrible ;)
JaredPar
Why was this changed from >= to =>?
Davy8
A: 

I'm not exactly sure what you're trying to do here, but strings directly translate to Linq to SQL queries.

Could you give an example of what you're attempting?

Here's a basic example usage:

string string2 = "test";

using (MyDataContext dc = new MyDataContext())
{
   // without lambdas
   var query1 = from item in dc.Items
               where item.Value == string2
               select item;

   // with lambdas
   var query2 = dc.Items.Where(item=>item.string1 == string2);
}
Code Monkey
+1  A: 

I am not quite sure what => than is or what language you are talking about, but I can only guess you are referring to >= (greater than or equal). You cannot use a greater than or equal operator with strings, because there is not definitive way to tell what you are talking about. If they are actually numbers you may want to do.

var query = from c in dc.Customers
            where c.CustomerID >= Int32.Parse("32")
            select c;
Nick Berardi
+3  A: 

If you're looking for => which would normally be written as >= then you cannot do this directly with strings. You can get the same behaviour via CompareTo:

string1.CompareTo(string2) >= 0

In this case, the result being less than or equal to zero means that string1 would be sorted before string2 and therefore is greater.

FYI the => operator in C# is only used in the definition of lambda expressions.

Drew Noakes
+1  A: 

Linq to SQL does not support String.Compare - see here: http://msdn.microsoft.com/en-us/library/bb882672.aspx

I am at a loss as to how to perform this type of comparison??? Any more ideas?

The String.Compare(string,string) is supported.
Jedi Master Spooky
If you wan to do is String.StartWith(). This is support but the sql is translate in a like - if you have a big table you have a problem. - So we do this. we substring the larger to the length of the first. Then we can use equals instead of like.
Jedi Master Spooky
A: 

As it turns out, I tried string.Compare anyway and it works fine - go figure...

A: 

I did a blog post about Binary Comparison with Linq To Sql that applies to string as well as any other SQL type.

For more information go to: http://diegofrata.wordpress.com/2009/11/18/linq-to-sql-expression-trees-and-binary-comparing/

Diego