我可以创建一个辅助函数来与Linq一起使用吗?

时间:2015-10-28 19:10:41

标签: c# linq

我有一个辅助功能:

namespace PublicationSystem.Tools
{
    public class Format
    {
        public static string FullName(string salutation, 
            string first, 
            string middle, 
            string last, 
            string suffix = "")
        {
            var fullName = new StringBuilder();

            if (!string.IsNullOrEmpty(salutation)) fullName.Append(salutation + " ");

            fullName.Append(first + " ");

            if (!string.IsNullOrEmpty(middle)) fullName.Append(middle.Trim() + " ");

            fullName.Append(last);

            if (!string.IsNullOrEmpty(suffix)) fullName.Append(" " + suffix);

            return fullName.ToString().Trim();
        }
    }
}

我很想在Linq查询中使用它,如下所示:

SessionUser = (
    from pi in db.ProfileIdentifier
    join p in db.Profile on pi.ProfileId equals p.ProfileId
    where
        pi.ProfileIdentifierTypeId == (int)ProfileIdentifierTypes.email &&
        pi.ProfileIdentifierValue == email &&
        pi.IsActive
    select new LoginViewModel
    {
        ProfileId = p.ProfileId,
        SessionId = sessionTokenId,
        FullName = Format.FullName(p.Salutation,p.FirstName,p.MiddleName,p.LastName),
        Email = email,
        Permissions = (
            from ra in db.RoleAssignment
            join rp in db.RolePermission on ra.RoleId equals rp.RoleId
            join perm in db.Permission on rp.PermissionId equals perm.PermissionId
            where ra.AssigneeId == p.ProfileId &&
                rp.IsActive
            select new ProfilePermission
            {
                RoleId = ra.RoleId,
                AssignedToId = ra.AssignedToId,
                PermissionId = perm.PermissionId,
                PermissionName = perm.PermissionName,
                PermissionGroupId = perm.PermissionGroupId
            }).ToList()
    }).FirstOrDefault();

有没有办法做到这一点?

3 个答案:

答案 0 :(得分:0)

是的,但是在查询数据源(例如SQL Server)时,该提供程序将不知道如何处理您的函数。

您可以在查询中使用.AsEnumerable(),以便在客户端而不是在数据库中评估该查询的其余部分。在调用.AsEnumerable()之后,您可以使用自己的函数,例如Format.FullName()。

请注意,在完成可在数据库中完成的过滤之前,应避免调用.AsEnumerable(),因为该调用之后的所有数据都会从数据库传输到客户端程序。

答案 1 :(得分:0)

Linq To Sql不知道如何将方法FullName(..)的内容解析为sql。

您可以执行查询,返回匿名对象,示例:

var tempUser = (
    from pi in db.ProfileIdentifier
    join p in db.Profile on pi.ProfileId equals p.ProfileId
    where
        pi.ProfileIdentifierTypeId == (int)ProfileIdentifierTypes.email &&
        pi.ProfileIdentifierValue == email &&
        pi.IsActive
    select new 
    {
        p.ProfileId,
        SessionId = sessionTokenId,
        p.Salutation,       
        p.FirstName,
        p.MiddleName,
        p.LastName,
        Email = email
    }).FirstOrDefault();

之后,您可以使用函数FullName(..)转换结果,样本:

if (tempUser != null)   
{
    SessionUser = new LoginViewModel
    {
        ProfileId = tempUser.ProfileId,
        SessionId = tempUser.SessionId,
        FullName = Format.FullName(tempUser.Salutation, tempUser.FirstName, tempUser.MiddleName, tempUser.LastName),
        Email = tempUser.Email
    };
}

答案 2 :(得分:-1)

这是最简单的转换:

SessionUser = (
    from pi in db.ProfileIdentifier
    join p in db.Profile on pi.ProfileId equals p.ProfileId
    where
        pi.ProfileIdentifierTypeId == (int)ProfileIdentifierTypes.email &&
        pi.ProfileIdentifierValue == email &&
        pi.IsActive
    select p)
    .ToList()
    .Select(p=>new LoginViewModel
    {
        ProfileId = p.ProfileId,
        SessionId = sessionTokenId,
        FullName = Format.FullName(p.Salutation,p.FirstName,p.MiddleName,p.LastName),
        Email = email
    }).FirstOrDefault();

这会更有效:

SessionUser = (
    from pi in db.ProfileIdentifier
    join p in db.Profile on pi.ProfileId equals p.ProfileId
    where
        pi.ProfileIdentifierTypeId == (int)ProfileIdentifierTypes.email &&
        pi.ProfileIdentifierValue == email &&
        pi.IsActive
    select p)
    .Take(1)
    .ToList()
    .Select(p=>new LoginViewModel
    {
        ProfileId = p.ProfileId,
        SessionId = sessionTokenId,
        FullName = Format.FullName(p.Salutation,p.FirstName,p.MiddleName,p.LastName),
        Email = email
    }).FirstOrDefault();

效率更高:

SessionUser = (
    from pi in db.ProfileIdentifier
    join p in db.Profile on pi.ProfileId equals p.ProfileId
    where
        pi.ProfileIdentifierTypeId == (int)ProfileIdentifierTypes.email &&
        pi.ProfileIdentifierValue == email &&
        pi.IsActive
    select new 
    {
        p.ProfileId,
        p.Salutation,       
        p.FirstName,
        p.MiddleName,
        p.LastName
    })
    .Take(1)
    .ToList()
    .Select(p=>new LoginViewModel
    {
        ProfileId = p.ProfileId,
        SessionId = sessionTokenId,
        FullName = Format.FullName(p.Salutation,p.FirstName,p.MiddleName,p.LastName),
        Email = email
    }).FirstOrDefault();