用户定义的函数(UDF)与动态LINQ到Sql? [英] User-defined Functions (UDF) with Dynamic LINQ to Sql?

查看:113
本文介绍了用户定义的函数(UDF)与动态LINQ到Sql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我希望使用Dynamic linq to SQL调用用户定义函数(UDF)。
我读到可以使用静态LINQ to Sql。


静态Linq的示例:

public void TestStaticLinq()
{
var query = new TestDBDataContext();
query.ISOweek(DateTime.Now);
}

这个带静态Linq的例子也适用:

public void TestUDF2()
{
var query = new TestDBDataContext();
var var = from query.myTables中的n query.ISOweek(Convert.ToDateTime(n.F9.Replace('。','/')))== 28
选择新的{F9 = n.F9};
}

使用Dynamic Linq进行示例调用?

public void TestDynamicLinq()
{
string c onnectionString = .....;
输入myType = ....;

var dxDynamic = new System.Data.Linq.DataContext(connectionString);

//这不起作用!!
var queryDynamic =
dxDynamic.GetTable(myType).Where(" ISOweek(Convert.ToDateTime(F9)) == 28 ");

---------------
System.Linq.Dynamic.ExpressionParser中的ParseException:
'myType'中不存在适用的方法'ISOweek'。"
---------------

这是可以理解的,因为myType还没有ISOweek定义的功能。

问题
我通过反射发射创建类型。
我是否必须扩展我的反射发射逻辑它包含我的UDF的方法定义还是有另一种方式?中文

解决方案

我认为不支持...你可以随时混合动态和非动态c linq,所以使用正常的.Where(表达式< Func< T,bool>>)为你的UDF提供东西。例如。如果需要,请将dynamic用于其他非UDF标准:

IQueryable query = dxDynamic.GetTable(myType).Where("somedynamicthing == @ 0",dynamicparam);

..然后对UDF的东西不动态:

if(myType == typeof(MyType))
{
表达式< Func< MyType,bool> ;> criteria =(f => f.ISOweek(F9)== 28);
query = query.WhereT(criteria);
}

(上面的whereT是扩展名)方法:

私人静态IQueryable WhereT< T>(此IQueryable查询,表达式< Func< T,bool>>标准)
{
return(IQueryable)((IQueryable< ; T>)查询)。所以(标准);
}



...然后你可以继续将更多动态linq内容应用于查询...

Hello,

i would like to call a user-defined functions (UDF) with Dynamic linq to SQL.
I read that it ist possible with static LINQ to Sql.


This example with static Linq works:

    public void TestStaticLinq()
    {
      var query = new TestDBDataContext();
      query.ISOweek(DateTime.Now);
    }

This example with static Linq works also:

    public void TestUDF2()
    {
      var query = new TestDBDataContext();
      var result = from n in query.myTables
                   where query.ISOweek(Convert.ToDateTime(n.F9.Replace('.','/'))) == 28
                   select new { F9 = n.F9};
    }

Example call with Dynamic Linq?

    public void TestDynamicLinq()
    {
      string connectionString = .....;
      Type myType =....;

      var dxDynamic = new System.Data.Linq.DataContext(connectionString);

// This does NOT WORK !!
      var queryDynamic =
dxDynamic.GetTable(myType).Where("ISOweek(Convert.ToDateTime(F9)) == 28 ");

---------------
ParseException in System.Linq.Dynamic.ExpressionParser:
"No applicable method 'ISOweek' exists in 'myType'."
---------------

This is understandable because myType does not yet has a function ISOweek defined.

QUESTION
I create the type via reflection emit.
Do I have to extend my reflection emit logic that it contains the method definitions for my UDF or is there another way?





解决方案

I don't think that's supported... You can always mix dynamic and non-dynamic linq, so use the normal .Where(Expression<Func<T, bool>>) thingie for your UDFs. E.g. use dynamic for other non-UDFs criteria if you need to:

IQueryable query = dxDynamic.GetTable(myType).Where("somedynamicthing == @0", dynamicparam);

..and then go non-dynamic for the UDF stuff:

if (myType == typeof(MyType))
{
  Expression<Func<MyType, bool>> criteria = (f => f.ISOweek(F9) == 28);
  query = query.WhereT(criteria);
}

(whereT above is an extension method:

private static IQueryable WhereT<T>(this IQueryable query, Expression<Func<T, bool>> criteria)
{
    return (IQueryable)((IQueryable<T>)query).Where(criteria);
}

)

...and then you can continue applying more dynamic linq stuff to the query...


这篇关于用户定义的函数(UDF)与动态LINQ到Sql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆