Linq实体 - Where子句中的表达函数

时间:2017-03-02 20:40:31

标签: sql-server entity-framework linq entity-framework-6 expression

我正在使用模式EntityServerModeDataSource(demo )进行数据绑定ASPxGridview我正在使用像数据源的实体框架(ef6),并且我正在使用Linq表达式加载网格:

    protected void EntityServerModeDataSource1_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
    e.QueryableSource = from emps in MyEntity.TableEntities
                                    join y in MyEntity.SubTableEntities
                                    on emps.IDTableEntities
                                    equals y.IDTableEntities
                                    join z in MyEntity.TblAcessProfiles on 1 equals z.IDAccessProfiles
                                    join z1 in MyEntity.TblAccess on z.IDAccess equals z1.IDAccess


                                    select new
                                    {
                                       emps.Field1,
                                       emps.Field2,
                                       emps.Field3,
                                       y.Field1,
                                       y.Field2
                                    };
    }

我需要使用当前我在SQL中的一些函数来过滤查询结果。我通过直接调用SQL(emdfunctions)来使用where子句中的函数:

 [DbFunction("EntityModel.Store", "GetAccess")]
    public static int GetAccess(string LogUserInfoAccess,string InfoEntidad,string AccessTextToFind)
    {

        throw new NotSupportedException("Direct calls are not supported.");
    }



  e.QueryableSource = (   from x in from emps in MyEntity.TableEntities
                          join y in MyEntity.SubTableEntities
                          on emps.IDTableEntities
                          equals y.IDTableEntities
                          join z in MyEntity.TblAcessProfiles          
                          on 1 equals z.IDAccessProfiles
                          join z1 in MyEntity.TblAccess 
                          on z.IDAccess equals z1.IDAccess             
                where ((z1.AccessTxt == "AccessName") &&
                         GetAccess(
                              "AccessOfUSerLog",
                              y.EntityInfo,
                              z.Access
                             )) == true
                         select new
                         {
                             emps.Field1,
                             emps.Field2,
                             emps.Field3,
                             y.Field1,
                             y.Field2
                         });

LogUSerInfoAccess字段包含要检查的用户的所有访问权限,     InfoEntidad的访问必须同意对loguser访问的相同访问。 AccessTextToFind是要查找的访问权限的名称。 SQL定义:

ALTER FUNCTION [dbo].[GetUserAccess](@LogUserInfo varchar(MAX), @CheckUserInfo varchar(MAX),@AccesoInfo varchar(MAX))

RETURNS位 如 BEGIN

DECLARE @contA int 
DECLARE @contB int
DECLARE @contC int
DECLARE @auxA int = 1
DECLARE @auxB int
DECLARE @auxC int
DECLARE @KeyA varchar(MAX)
DECLARE @KeyB varchar(MAX)
DECLARE @KeyC varchar(MAX)
DECLARE @auxOK int = 0
DECLARE @auxClaves int = 0

SET @contA = convert(int, [dbo].[SplitASG](@AccesoInfo,';',0))

WHILE @auxA <= @contA
BEGIN
    SET @KeyA = dbo.splitASG(@AccesoInfo,';',@auxA)
    SET @auxB = 1
    SET @contB = convert(int, [dbo].[SplitASG](@LogUserInfo,';',0))
    WHILE @auxB <= @contB
    BEGIN
        SET @KeyB = dbo.splitASG(@LogUserInfo,';',@auxB)
        IF [dbo].[SplitASG](@KeyB,'=',1) = [dbo].[SplitASG](@KeyA,'=',1) 
        BEGIN
            SET @auxC = 1
            SET @contC = convert(int, [dbo].[SplitASG](@CheckUserInfo,';',0))
            WHILE @auxC <= @contC
            BEGIN
                SET @KeyC = dbo.splitASG(@CheckUserInfo,';',@auxC)
                IF [dbo].[SplitASG](@KeyC,'=',1) = [dbo].[SplitASG](@KeyA,'=',1) 
                BEGIN
                    SET @auxClaves = @auxClaves + 1
                    IF (CHARINDEX(','+[dbo].[SplitASG](@KeyB,'=',2)+',', ','+[dbo].[SplitASG](@KeyA,'=',2)+',') > 0 AND CHARINDEX(','+[dbo].[SplitASG](@KeyC,'=',2)+',', ','+[dbo].[SplitASG](@KeyA,'=',2)+',') > 0)
                    OR CHARINDEX(',-1,', ','+[dbo].[SplitASG](@KeyA,'=',2)+',') > 0
                    OR (CHARINDEX(',-2,', ','+[dbo].[SplitASG](@KeyA,'=',2)+',') > 0 and CHARINDEX(','+[dbo].[SplitASG](@KeyB,'=',2)+',', ','+[dbo].[SplitASG](@KeyC,'=',2)+',') > 0)
                    SET @auxOK = @auxOK + 1
                END
                SET @auxC = @auxC + 1
            END
        END
        SET @auxB = @auxB + 1
    END

    SET @auxA = @auxA + 1
END
    RETURN case when @auxOK = @auxClaves then 1 ELSE 0 END END

没关系,但表现非常糟糕。我认为我的解决方案是c sharp中的函数,并从linq查询的where子句调用它们。 所以我把这个函数传递给了.NET:

    public static int GetUserAccess(string LogUserInfo, string CheckUserInfo, string AccesoInfo)
{
    int ContA;
    int ContB;
    int ContC;
    int AuxA = 1;
    int AuxB;
    int AuxC;
    string KeyA;
    string KeyB;
    string KeyC;
    int auxOK = 0;
    int auxClaves = 0;


    ContA = Convert.ToInt32(SplitASG(AccesoInfo, ";", 0));
    while (AuxA <= ContA)
    {
        KeyA = SplitASG(AccesoInfo, ";", AuxA);
        AuxB = 1;
        ContB = Convert.ToInt32(SplitASG(LogUserInfo, ";", 0));
        while (AuxB <= ContB)
        {
            KeyB = SplitASG(LogUserInfo, ";", AuxB);
            if (SplitASG(KeyB, "=", 1) == SplitASG(KeyA, "=", 1))
            {
                AuxC = 1;
                ContC = Convert.ToInt32(SplitASG(CheckUserInfo, ";", 0));
                while (AuxC <= ContC)
                {
                    KeyC = SplitASG(@CheckUserInfo, ";", AuxC);
                    if (SplitASG(KeyC, "=", 1) == SplitASG(KeyA, "=", 1))
                    {


                        auxClaves += 1;

                        if ((("," + SplitASG(KeyA, "=", 2) + ",").IndexOf("," + SplitASG(KeyB, "=", 2) + ",") > -1
                    &
                    ("," + SplitASG(KeyA, "=", 2) + ",").IndexOf("," + SplitASG(KeyC, "=", 2) + ",") > -1)

                    | (",-1,").IndexOf("," + SplitASG(KeyA, "=", 2) + ",") > -1

                    | ((",-2,").IndexOf("," + SplitASG(KeyA, "=", 2) + ",") > -1 &

                    ("," + SplitASG(KeyC, "=", 2) + ",").IndexOf("," + SplitASG(KeyB, "=", 2) + ",") > -1

                    ))
                        {
                            auxOK += 1;
                        }
                    }
                    AuxC += 1;
                }
            }
            AuxB += 1;
        }
        AuxA += 1;
    }
    return auxOK == auxClaves ? 1 : 0;
}

我知道从表达式函数调用.NET函数的唯一方法是,我搜索了很多文档,但是我无法正确调用它。

    var query = (from emps in MyEntity.TableEntities
                                    join y in MyEntity.SubTableEntities
                                    on emps.IDTableEntities
                                    equals y.IDTableEntities
                                    join z in MyEntity.TblAcessProfiles on 1 equals z.IDAccessProfiles
                                    join z1 in MyEntity.TblAccess on z.IDAccess equals z1.IDAccess


                                    select new ClassResult
                                    {
                                       emps.Field1,
                                       emps.Field2,
                                       emps.Field3,
                                       y.Field1,
                                       y.Field2
                                    }
).AsQueryable().Where(GetAccess);

 public Expression<Func<ClassResult, bool>> GetAccess
{                                      
  return e => e.Field.. ;       
}

我无法从函数表达式中调用函数,我不能在函数体中放置多行...我需要一些文档或一些示例,在linq中使用我需要的地方,道歉这么长的问题。 问候和谢谢

0 个答案:

没有答案