Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询“元素”或IQueryable时的Linq通用表达式(多用途)_C#_.net_Linq_Linq To Sql - Fatal编程技术网

C# 查询“元素”或IQueryable时的Linq通用表达式(多用途)

C# 查询“元素”或IQueryable时的Linq通用表达式(多用途),c#,.net,linq,linq-to-sql,C#,.net,Linq,Linq To Sql,我有下面的表达 public static Expression<Func<T, bool>> JoinByDateCheck<T>(T entity, DateTime dateToCheck) where T : IDateInterval { return (entityToJoin) => entityToJoin.FromDate.Date <= dateToCheck.Date && (enti

我有下面的表达

public static Expression<Func<T, bool>> JoinByDateCheck<T>(T entity, DateTime dateToCheck) where T : IDateInterval
{
    return (entityToJoin) => 
        entityToJoin.FromDate.Date <= dateToCheck.Date && (entityToJoin.ToDate == null || entityToJoin.ToDate.Value.Date >= dateToCheck.Date);
}
我需要从几个方面应用它:

1查询Linq2Sql表:

var q1=从intervalTable中的e开始,其中函数调用JoinByDateChecke,constantDateTime选择e

或者像这样:

interface IDateInterval 
{
    DateTime FromDate {get;}
    DateTime? ToDate {get;}
}
intervalTable.Where函数调用JoinByDateChecke,constantDateTime

2我需要在一些表联接中使用它,因为linq2sql不提供比较联接:

var q2=从t1中的e1在e1.FK上连接t2中的e2==e2.PK,其中调用JoinByDateChecke2、e1.FromDate的其他函数

var q2=从t1中的e1到t2中的e2,其中e1.FK==e2.PK和调用JoinByDateChecke2、e1.FromDate的其他函数

3我需要在一些查询中使用它,例如:

var q3=调用JoinByDate CheckConstantDate的intervalTable.FilterFunction中的e

动态linq不是我可以使用的东西,所以我必须坚持使用普通linq

多谢各位

澄清:

最初,我只有最后一个方法FilterFunction调用JoinByDateCheckthis IQueryable实体,即DateTime dateConstant,它包含表达式中的代码

问题是,如果我在一个方法中编写代码并这样调用它,我会得到一个SQL Translate异常


我只想将此函数的使用扩展到where子句。请参见第2点中的第二个查询,您将无法在查询表达式中使用它,但您的第二个代码段调用where EXPLICIT应该可以


对于第二个查询,很难说没有看到其他调用JoinByDateCheck的函数-基本上,您无法将其放入查询表达式中,但如果您可以将查询传递给另一个函数,这可能会起作用。

好的,我的想法是有很多代码必须调用此条件,而且它必须被用作standard@Bogdan:问题是查询表达式会自动将其中的代码转换为lambda表达式,对于LINQ到SQL,lambda表达式随后会转换为表达式树。您不希望表达式树包含对helper方法的调用—您希望调用helper方法本身。这在查询表达式中根本不起作用