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

本文关键字:Linq 函数 一起 创建 一个 我可以 | 更新日期: 2023-09-27 18:19:44

我有一个助手函数:

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();

有办法做到这一点吗?

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

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

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

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

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
    };
}

这是最简单的转换:

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();