Custom包含类似于LINQ to SQL生成查询的方法实现

时间:2016-08-01 10:00:12

标签: c# sql-server entity-framework linq linq-to-sql

我正在使用EF6进行数据模型,并且我必须使用LINQ to SQL和通用存储库,我们生成一些查询来过滤数据并使用Contains执行过滤器,它使用CHARINDEX生成查询,现在我想要实现相同的包含功能,包括特定的Collat​​e,

   var contactFilterPredicate = PredicateBuilder.True<CON_CONTACT>();
        if (!string.IsNullOrWhiteSpace(contactRequest.Email))
        {
            contactFilterPredicate = contactFilterPredicate.And(x => x.CON_EMAIL.Contains(contactRequest.Email));
        }
        if (!string.IsNullOrWhiteSpace(contactRequest.Name))
        {
            contactFilterPredicate = contactFilterPredicate.And(x => x.CON_FIRST_NAME.Contains(contactRequest.Name) || x.CON_SURNAME.Contains(contactRequest.Name));
        }
        return contactFilterPredicate;

具有SQL输出

SELECT
[GroupBy1].[A1] AS [C1] FROM (SELECT
    COUNT(1) AS [A1]
FROM [dbo].[CON_CONTACT] AS [Extent1]
WHERE ([Extent1].[CON_FIRST_NAME] LIKE @p__linq__0 ESCAPE N'~')
OR ([Extent1].[CON_SURNAME] LIKE @p__linq__1 ESCAPE N'~')) AS [GroupBy1]

我想用MyContains用Collat​​e生成它

   var contactFilterPredicate = PredicateBuilder.True<CON_CONTACT>();
        if (!string.IsNullOrWhiteSpace(contactRequest.Email))
        {
            contactFilterPredicate = contactFilterPredicate.And(x => x.CON_EMAIL.MyContains(contactRequest.Email));
        }
        if (!string.IsNullOrWhiteSpace(contactRequest.Name))
        {
            contactFilterPredicate = contactFilterPredicate.And(x => x.CON_FIRST_NAME.MyContains(contactRequest.Name) || x.CON_SURNAME.MyContains(contactRequest.Name));
        }
        return contactFilterPredicate;

我希望生成类似

SELECT
[GroupBy1].[A1] AS [C1] FROM (SELECT
    COUNT(1) AS [A1]
FROM [dbo].[CON_CONTACT] AS [Extent1]
WHERE ([Extent1].[CON_FIRST_NAME] COLLATE SQL_Latin1_General_CP1253_CI_AI LIKE @p__linq__0 ESCAPE N'~')
OR ([Extent1].[CON_SURNAME] COLLATE SQL_Latin1_General_CP1253_CI_AI LIKE @p__linq__1 ESCAPE N'~')) AS [GroupBy1]

有可能创造吗?如果是,那怎么样?

0 个答案:

没有答案