嵌套 LINQ 方法抛出“不支持...”异常

发布于 2024-08-07 05:20:17 字数 2497 浏览 7 评论 0原文

这是这里的后续 -->multiple-sorting-on-linq-nested -方法

基本上,在 let memberName = ... 上,它抛出此异常 Method 'System.String MemberName(Int32)' has nosupported conversion to SQL. 并且我不知道出解决方案。

此外,BLLCmoBLLConnect 实际上使用两个不同的数据库。原来的应用程序(不是我的)使用 4 个独立的数据库,所以我想尽办法。


BLLCmo.cs

    public static DataTable GetAllMembers(Guid workerID)
    {
        var AllEnrollees = from enrollment in context.tblCMOEnrollments
                               where enrollment.CMOSocialWorkerID == workerID || enrollment.CMONurseID == workerID
                               join supportWorker in context.tblSupportWorkers on enrollment.EconomicSupportWorkerID equals supportWorker.SupportWorkerID into workerGroup
                               from worker in workerGroup.DefaultIfEmpty()
                               let memberName = BLLConnect.MemberName(enrollment.ClientID)
                               orderby enrollment.DisenrollmentDate ascending, memberName ascending 
                               select new
                                       {
                                           enrollment.ClientID,
                                           MemberName = memberName,
                                           NurseName = BLLAspnetdb.NurseName(enrollment.CMONurseID),
                                           SocialWorkerName =BLLAspnetdb.SocialWorkerName(enrollment.CMOSocialWorkerID),
                                           enrollment.DisenrollmentDate,
                                           enrollment.EnrollmentDate,
                                           ESFirstName = worker.FirstName,
                                           ESLastName = worker.LastName,
                                           ESPhone = worker.Phone
                                       };

        var dataTable = AllEnrollees.CopyLinqToDataTable();

        return dataTable;
    }

BLLConnect.cs

    public static String MemberName(Int32 personID)
    {
        var memberName = from person in context.tblPersons
                         where person.PersonID == personID
                         select person.FirstName + " " + person.LastName;

        return memberName.SingleOrDefault();
    }

This is a follow up from here -->multiple-sorting-on-linq-nested-method .

Basically, on let memberName = ... it is throwing this exception Method 'System.String MemberName(Int32)' has no supported translation to SQL. and I am not figuring out the solution.

Also, BLLCmo and BLLConnect actually use TWO different DataBases. The original app(not mine) uses 4 Seperate DB's so I am trying to make due.


BLLCmo.cs

    public static DataTable GetAllMembers(Guid workerID)
    {
        var AllEnrollees = from enrollment in context.tblCMOEnrollments
                               where enrollment.CMOSocialWorkerID == workerID || enrollment.CMONurseID == workerID
                               join supportWorker in context.tblSupportWorkers on enrollment.EconomicSupportWorkerID equals supportWorker.SupportWorkerID into workerGroup
                               from worker in workerGroup.DefaultIfEmpty()
                               let memberName = BLLConnect.MemberName(enrollment.ClientID)
                               orderby enrollment.DisenrollmentDate ascending, memberName ascending 
                               select new
                                       {
                                           enrollment.ClientID,
                                           MemberName = memberName,
                                           NurseName = BLLAspnetdb.NurseName(enrollment.CMONurseID),
                                           SocialWorkerName =BLLAspnetdb.SocialWorkerName(enrollment.CMOSocialWorkerID),
                                           enrollment.DisenrollmentDate,
                                           enrollment.EnrollmentDate,
                                           ESFirstName = worker.FirstName,
                                           ESLastName = worker.LastName,
                                           ESPhone = worker.Phone
                                       };

        var dataTable = AllEnrollees.CopyLinqToDataTable();

        return dataTable;
    }

BLLConnect.cs

    public static String MemberName(Int32 personID)
    {
        var memberName = from person in context.tblPersons
                         where person.PersonID == personID
                         select person.FirstName + " " + person.LastName;

        return memberName.SingleOrDefault();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2024-08-14 05:20:17

问题在于 LINQ to SQL 正在尝试将您的方法转换为 SQL。由于 MemberName 不是有效的 SQL,因此它放弃。相反,您需要从 SQL 中提取所需的数据,然后在单独的 LINQ to Objects 查询中调用方法(并排序):

public static DataTable GetAllMembers(Guid workerID)
{
    var AllEnrollees =
        from enrollment in context.tblCMOEnrollments
        where enrollment.CMOSocialWorkerID == workerID || enrollment.CMONurseID == workerID
        join supportWorker in context.tblSupportWorkers on enrollment.EconomicSupportWorkerID equals supportWorker.SupportWorkerID into workerGroup
        from worker in workerGroup.DefaultIfEmpty()
        select new
        {
            enrollment.ClientID,
            enrollment.CMONurseID,
            enrollment.CMOSocialWorkerID,
            enrollment.DisenrollmentDate,
            enrollment.EnrollmentDate,
            ESFirstName = worker.FirstName,
            ESLastName = worker.LastName,
            ESPhone = worker.Phone
        };

    var result =
        from enrollee in AllEnrollees.AsEnumerable()
        let memberName = BLLConnect.MemberName(enrollee.ClientID)
        orderby enrollee.DisenrollmentDate ascending, memberName ascending 
        select new
        {
            enrollee.ClientID,
            MemberName = memberName,
            NurseName = BLLAspnetdb.NurseName(enrollee.CMONurseID),
            SocialWorkerName = BLLAspnetdb.SocialWorkerName(enrollee.CMOSocialWorkerID),
            enrollee.DisenrollmentDate,
            enrollee.EnrollmentDate,
            enrollee.ESFirstName,
            enrollee.ESLastName,
            enrollee.ESPhone
        };

    return result.CopyLinqToDataTable();
}

The problem is that LINQ to SQL is trying to translate your method into SQL. Since MemberName isn't valid SQL, it gives up. Instead, you'll need to pull down the data you need from SQL and then call your methods (and sort) in a separate LINQ to Objects query:

public static DataTable GetAllMembers(Guid workerID)
{
    var AllEnrollees =
        from enrollment in context.tblCMOEnrollments
        where enrollment.CMOSocialWorkerID == workerID || enrollment.CMONurseID == workerID
        join supportWorker in context.tblSupportWorkers on enrollment.EconomicSupportWorkerID equals supportWorker.SupportWorkerID into workerGroup
        from worker in workerGroup.DefaultIfEmpty()
        select new
        {
            enrollment.ClientID,
            enrollment.CMONurseID,
            enrollment.CMOSocialWorkerID,
            enrollment.DisenrollmentDate,
            enrollment.EnrollmentDate,
            ESFirstName = worker.FirstName,
            ESLastName = worker.LastName,
            ESPhone = worker.Phone
        };

    var result =
        from enrollee in AllEnrollees.AsEnumerable()
        let memberName = BLLConnect.MemberName(enrollee.ClientID)
        orderby enrollee.DisenrollmentDate ascending, memberName ascending 
        select new
        {
            enrollee.ClientID,
            MemberName = memberName,
            NurseName = BLLAspnetdb.NurseName(enrollee.CMONurseID),
            SocialWorkerName = BLLAspnetdb.SocialWorkerName(enrollee.CMOSocialWorkerID),
            enrollee.DisenrollmentDate,
            enrollee.EnrollmentDate,
            enrollee.ESFirstName,
            enrollee.ESLastName,
            enrollee.ESPhone
        };

    return result.CopyLinqToDataTable();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文