返回不同的对象,一个LINQtoSQL声明 [英] Return different objects with one LINQtoSQL statement

查看:123
本文介绍了返回不同的对象,一个LINQtoSQL声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题:我有一个由员工和学生实施的IPerson。我的真正的希望就是你看到下面的东西。一个LINQ语句获取每种类型的IPerson的。直到我调用该方法这个伟大工程)。这是有道理的,为什么我会得到错误,但我真的很挣扎找到一个体面的方式来拉从数据库中的所有IPerson对象,并避免把switch语句都在我的应用程序。

Here's my problem: I have an IPerson which is implemented by Employee and Student. What I really want is what you see below. One LINQ statement to get each type of IPerson. This works great until I call the method ;). It makes sense as to why I'd get the error, but I am really struggling as to find a decent way to pull all IPerson objects from the DB and avoid putting switch statements all over my application.

public IQueryable<IPerson> getPersons() {

        // gives Types in Union or Concat have different members assigned error

        var people = from p in db.Persons select p;

        var students = (from s in people
                        where s.TypeId == (int)PersonType.Student
                        select new Student
                        {
                            Id = s.Id,
                            Age = s.Age.GetValueOrDefault(0),
                            Name = s.Name,
                            Major = s.Student.Major ?? "None",
                            CreditHours = s.Student.CreditHours.GetValueOrDefault(0),
                            PersonType = (PersonType)s.TypeId
                        }).Cast<IPerson>();
        var employees = (from e in people
                        where e.TypeId == (int)PersonType.Employee
                        select new Employee
                        {
                            Id = e.Id,
                            Age = e.Age.GetValueOrDefault(0),
                            Name = e.Name,
                            PersonType = (PersonType)e.TypeId,
                            Salary = e.Employee.Salary.GetValueOrDefault(0)
                        }).Cast<IPerson>();

        return students.Concat<IPerson>(employees);
        //return (students.ToList()).Concat<IPerson>(employees.Cast<IPerson>().ToList()).AsQueryable<IPerson>();
    }

上面有一个注释掉return语句 - 基本上做了.ToList()和内放弃整个延迟执行的事情,创建2条SQL语句 - 不理想

Above, there is a commented out return statement - that essentially does a .ToList() and forgoes the whole deferred execution thing, creating 2 SQL statements - not ideal.

任何帮助AP preciated!

Any help is appreciated!

推荐答案

这个怎么样:

public IQueryable<IPerson> getPersons() {

    // gives Types in Union or Concat have different members assigned error

    var people = from p in db.Persons select p;

    return (from s in people
                    where s.TypeId == (int)PersonType.Student
                    select new Student
                    {
                        Id = s.Id,
                        Age = s.Age.GetValueOrDefault(0),
                        Name = s.Name,
                        Major = s.Student.Major ?? "None",
                        CreditHours = s.Student.CreditHours.GetValueOrDefault(0),
                        PersonType = (PersonType)s.TypeId
                    }).Cast<IPerson>().Union((from e in people
                        where e.TypeId == (int)PersonType.Employee
                        select new Employee
                        {
                            Id = e.Id,
                            Age = e.Age.GetValueOrDefault(0),
                            Name = e.Name,
                            PersonType = (PersonType)e.TypeId,
                            Salary = e.Employee.Salary.GetValueOrDefault(0)
                        }).Cast<IPerson>());
}

这不是好多了,但是你得到它的一个电话。或者,我会做的是这样的:

It's not much better, but you get it in one call. Or, what I would do is something like this:

public IPerson GetPerson(Person p) //I'm guessing that the objects in collection db.Persons is of type Person
{
    IPerson ret;
    switch(p.TypeId)
    {
        case (int)PersonType.Student: ret = .......break;
        case (int)PersonType.Employee: ret = ......break;
    }
    return ret;
}

public IQueryable<IPerson> getPersons() {
    return (from p in db.Persons select p).ToList().Select(p => GetPerson(p)).AsQueryable();
}

但话又说回来你得到的switch语句。另外,如果你不喜欢在DB做了ToList()(如果我没有记错LinqToSQL不支持使用与构造函数的变量),你可以尝试添加方法GetPerson(我大概虽然改名吧)由LinqToSQL(局部类)产生的Person类,但我不能确定这是合法的。

But then again you get the switch-statement. Also, if you don't like to do a ToList() on the DB (if I recall correctly LinqToSQL doesn't support functions using constructors with variables) you can try adding the method GetPerson (I'd probably rename it though) to the Person class generated by LinqToSQL (partial class), but I'm not certain this is legal.

但是,你打算如何使用的IQueryable从getPersons未来不使用开关,我就不知道了。

But how you're going to use the IQueryable coming from getPersons without using switch, I don't know.

这篇关于返回不同的对象,一个LINQtoSQL声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆