0

我想得到一个;一个人拥有的所有亲属的分隔字符串。涉及四个表:

USERTAB PERSON PERSON_RELATION RELATION_TAB

询问

from u in USERTAB
select new
{
    Person = from p in PERSON where p.USERID == u.USERID
       select new
        {
          PNo = p.NO,
          Name = p.NAME
          Relatives = (from r in PERSON_RELATION where r.PSEQ == p.PSEQ select new
          {
            Description = (from rel in RELATION_TYPE where rel.TYPE_SEQ == r.TYPE_SEQ select rel.DESCRIPTION).ToArray() //(or also tried .ToString())
          })                                     
        }
}

我希望描述字段是“;” 用户(人)拥有的所有亲属的分隔列表。

在我的 Relatives 对象上使用 ToString 它只会在运行时失败。LINQ to Entities 无法识别方法“System.String ToString()”方法

示例:描述 =“父亲、兄弟、表弟”

4

2 回答 2

2

您需要清楚查询的哪些部分被转换为 SQL 以在服务器上运行,以及哪些部分在本地应用程序中运行。关键是构建一个简单的查询来检索所有数据,然后使用.AsEnumerable()以确保剩余的转换不会被转换为 SQL,最后将数据转换为对您有用的形式。就像是

var query =
    from u in USERTAB
    select new
    {
        Person =
            from p in PERSON
            where p.USERID == u.USERID
            select new
            {
                PNo = p.NO,
                Name = p.NAME
                Relatives =
                    from r in PERSON_RELATION
                    where r.PSEQ == p.PSEQ
                    select new
                    {
                        Description =
                            from rel in RELATION_TYPE
                            where rel.TYPE_SEQ == r.TYPE_SEQ
                            select pos.DESCRIPTION
                    }
            }
        }
    };

var enumerable =
    from u in query.AsEnumerable()
    select new
    {
        Person =
            from p in u.Person
            select new
            {
                PNo = p.PNo,
                Name = p.Name
                Relatives =
                    string.Join(", ",
                        from r in p.Relatives
                        from d in r.Description
                        select d.Description)
            }
        }
    };

应该做的伎俩。

于 2012-12-03T11:46:47.347 回答
0

这应该有效:为数组字符串编写扩展方法,如下所示:

public static ToCsv(this string[] strings)
{
    return String.Join("," strings);
}

然后在上面.ToCsv()的通话结束时添加.ToArray(),它应该可以解决问题!

于 2012-12-03T11:36:25.803 回答