Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

只会vb,报错未实现函数表达式 CompareString解析 #140

Closed
cnxo2atjl opened this issue Nov 22, 2019 · 11 comments
Closed

只会vb,报错未实现函数表达式 CompareString解析 #140

cnxo2atjl opened this issue Nov 22, 2019 · 11 comments

Comments

@cnxo2atjl
Copy link

@cnxo2atjl cnxo2atjl commented Nov 22, 2019

sqlite
Dim xxx = Fsql.Select(Of 部门).Where(Function(yy) yy.ID = 1).ToList
这个没问题

Dim xxx = Fsql.Select(Of 部门).Where(Function(yy) yy.部门名 = "财务").ToList

报错如下:
System.Exception
HResult=0x80131500
Message=未实现函数表达式 CompareString(yy.部门名, "财务", False) 解析
Source=FreeSql

@2881099

This comment has been minimized.

Copy link
Owner

@2881099 2881099 commented Nov 22, 2019

好的,确实实现这个函数,问题是 c# 没得这个方法 CompareString,解决不了

.Where(Function(yy) yy.部门名.Equals("财务")).ToList

有没有这个方法?

或者看文档弄个自定义函数

自定义表达式函数(v0.11.23)

[ExpressionCall]
public static class DbFunc
{
    //必要定义 static + ThreadLocal
    static ThreadLocal<ExpressionCallContext> context = new ThreadLocal<ExpressionCallContext>();

    public static string FormatDateTime(this DateTime that, string arg1)
    {
        var up = context.Value;
        if (up.DataType == FreeSql.DataType.Sqlite)
            return $"date_format({up.Values["that"]}, {up.Values["arg1"]})";
        return "";
    }
    
    //...在此类中定义更多方法
}

var sql = fsql.Select<SysModule>()
    .ToSql(a => a.CreateTime.FormatDateTime("yyyy-MM-dd"));
//SELECT date_format(a."CreateTime", 'yyyy-MM-dd') as1 
//FROM "SysModule" a

context.Value.DataType 是 FreeSql.DataType 枚举类型,以便实现不同数据库的适配;

context.Value.Values 是 函数的各参数解析结果;

@pjy612

This comment has been minimized.

Copy link

@pjy612 pjy612 commented Nov 25, 2019

感觉他得拿VB去实现这个,能搞的定吗?_(:з」∠)_

@fenfei100

This comment has been minimized.

Copy link

@fenfei100 fenfei100 commented Dec 14, 2019

sqlite
Dim xxx = Fsql.Select(Of 部门).Where(Function(yy) yy.ID = 1).ToList
这个没问题

Dim xxx = Fsql.Select(Of 部门).Where(Function(yy) yy.部门名 = "财务").ToList

报错如下:
System.Exception
HResult=0x80131500
Message=未实现函数表达式 CompareString(yy.部门名, "财务", False) 解析
Source=FreeSql

不要用等号比较,用 Equals 就没有问题。我之前就发现这个问题,按我们的思维 C# == 转到 VB.Net 用 = 就可以了,但是在 freesql 中没有实现转换,所以只能用 Equals

@2881099

This comment has been minimized.

Copy link
Owner

@2881099 2881099 commented Dec 14, 2019

现在用 vb 的人这么多吗

@fenfei100

This comment has been minimized.

Copy link

@fenfei100 fenfei100 commented Dec 14, 2019

应该还是有吧,我觉得也不是多的问题,是个习惯问题吧。只能说 C#的大环境比VB.net 要好,加上微软的扶持。但是并不说就没什么有人用VB.net 了。

@2881099

This comment has been minimized.

Copy link
Owner

@2881099 2881099 commented Dec 14, 2019

未实现函数表达式 CompareString(yy.部门名, "财务", False) 解析

我说为什么表达式里不能用赋值。。。可能是考虑了 vb.net 那边的表达式赋值

@2881099

This comment has been minimized.

Copy link
Owner

@2881099 2881099 commented Dec 14, 2019

其实我最早也是写 vb6 入门的,后来慢慢就转到 c# 了

@fenfei100

This comment has been minimized.

Copy link

@fenfei100 fenfei100 commented Dec 14, 2019

那是你转型成功了,我就失败了

@2881099

This comment has been minimized.

Copy link
Owner

@2881099 2881099 commented Dec 15, 2019

image

Imports System

Module Program
    Sub Main(args As String())
        Console.WriteLine("Hello World!")

        Dim fsql = New FreeSql.FreeSqlBuilder() _
            .UseConnectionString(FreeSql.DataType.Sqlite, "data source=testvb.db") _
            .UseAutoSyncStructure(True) _
            .UseMonitorCommand(Sub(cmd) Trace.WriteLine(cmd.CommandText)) _
        .Build()

        REM Microsoft.VisualBasic.CompilerServices.Operators.CompareString()

        Dim List1 = fsql.Select(Of Testvb).Where(Function(a) a.Id = 100).ToList()
        REM SELECT a."Id", a."Title" FROM "Testvb" a WHERE (a."Id" = 100)

        Dim List2 = fsql.Select(Of Testvb).Where(Function(a) a.Title = "xxx").ToList()
        REM SELECT a."Id", a."Title" FROM "Testvb" a WHERE (a."Title" = 'xxx')

        Dim List3 = fsql.Select(Of Testvb).Where(Function(a) a.Title <> "xxx").ToList()
        REM SELECT a."Id", a."Title" FROM "Testvb" a WHERE (a."Title" <> 'xxx')

        fsql.Dispose()
    End Sub
End Module

Class Testvb
    Property Id As Integer
    Property Title As String
End Class

@fenfei100 @cnxo2atjl
好多年没写 vb 代码了,下个版本将支持 string 的 = 和 <> 解析

@densen2014

This comment has been minimized.

Copy link

@densen2014 densen2014 commented Dec 16, 2019

不用支持了, 反正已经摸通, :-)

2881099 pushed a commit that referenced this issue Dec 16, 2019
@fenfei100

This comment has been minimized.

Copy link

@fenfei100 fenfei100 commented Dec 17, 2019

标特否!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.