By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,417 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,417 IT Pros & Developers. It's quick & easy.

How to use CompareString() method

P: n/a
I added reference to Microsoft.VisualBasic.dll to project.

Line

var r = Microsoft.VisualBasic.CompilerServices.CompareStri ng("a", "b",
true)<1;

causes compile error:

The type or namespace name 'CompareString' does not exist in the namespace
'Microsoft.VisualBasic.CompilerServices' (are you missing an assembly
reference?)

According to MSDN doc this method must exist in this assembly.

How to use this method from C# ?
I need to create linq expression tree which calls this method.

Andrus.

Jun 30 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Andrus wrote:
I added reference to Microsoft.VisualBasic.dll to project.

Line

var r = Microsoft.VisualBasic.CompilerServices.CompareStri ng("a", "b",
true)<1;

causes compile error:

The type or namespace name 'CompareString' does not exist in the namespace
'Microsoft.VisualBasic.CompilerServices' (are you missing an assembly
reference?)

According to MSDN doc this method must exist in this assembly.
A method belongs to a class, not an assembly. It's a member of the Operators
class (which does reside in that assembly).
How to use this method from C# ?
You don't.
I need to create linq expression tree which calls this method.
I sincerely doubt that. There's a reason this method is flagged as "this API
supports the .NET Framework infrastructure and is not intended to be used
directly from your code."

I hesitate to ask why you think you need to create an expression tree that
uses Visual Basic internals in C#.

--
J.
Jun 30 '08 #2

P: n/a
Andrus <ko********@hot.eewrote:
I added reference to Microsoft.VisualBasic.dll to project.

Line

var r = Microsoft.VisualBasic.CompilerServices.CompareStri ng("a", "b",
true)<1;

causes compile error:

The type or namespace name 'CompareString' does not exist in the namespace
'Microsoft.VisualBasic.CompilerServices' (are you missing an assembly
reference?)

According to MSDN doc this method must exist in this assembly.

How to use this method from C# ?
I need to create linq expression tree which calls this method.
CompilerServices is a namespace - methods don't exist directly in
namespaces. CompareString is a method in the Operators class.

Why do you think you need to call it though? What's wrong with
String.CompareTo?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 30 '08 #3

P: n/a
I sincerely doubt that. There's a reason this method is flagged as "this
API supports the .NET Framework infrastructure and is not intended to be
used directly from your code."
That statement appears in many places in the docs but it's not always
trustworthy. It's part of a deeper problem with the .NET docs however which
are fundamentally very weak.
Jul 1 '08 #4

P: n/a
Jona and Jeroen,
Why do you think you need to call it though? What's wrong with
String.CompareTo?
I need to generate dynamically sql select statement which compares strings:

SELECT COUNT(*)
FROM Customer
WHERE CustomerId < 'AIRBU';

I expect that

Db.Customers.Where( c =>
Microsoft.VisualBasic.CompilerServices.Operators.C ompareString(
c.CustomerId, "AIRBU", false ) < 1 ).Count()

generates correct sql .

I'm not sure that String.CompareTo generates correct statement.

So I created following extension method based on Marc code samples:

public static IQueryable<TLessThanOrEqual<T>(this IQueryable<T>
source, string property, object value)
{
ParameterExpression obj = Expression.Parameter(typeof(T), "x");
Expression val = Expression.Constant(value, typeof(string));
Expression prop = Expression.Property(obj, property);
Expression call = Expression.Call(
typeof(Microsoft.VisualBasic.CompilerServices.Oper ators).GetMethod("CompareString",
new[] { typeof(string), typeof(string), typeof(bool) }), prop,
val,
Expression.Constant(false, typeof(bool)));
BinaryExpression testExp = LambdaExpression.LessThan(call,
Expression.Constant(1, typeof(int))
);
return source.Where(Expression.Lambda<Func<T, bool>>(testExp,
obj));
}

Is this best solution ?

Andrus.

Jul 1 '08 #5

P: n/a
String.Compare should work just fine (assuming you get the sign the
right way, which always makes me have to stop and think...); example
using NWind:

int cnt = (from cust in ctx.Customers
where string.Compare(cust.CompanyName,"abc") < 0
select cust).Count();
/*
SELECT COUNT(*) AS [value]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[CompanyName] < @p0
*/

Marc
Jul 1 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.