简体   繁体   English

将LINQ返回类型转换为C#类

[英]Convert LINQ return type to C# class

I have a LINQ query against my DBModel classes which is returning an anonymous type. 我对我的DBModel类有一个LINQ查询,该查询返回一个匿名类型。 I have to convert it to a known C# class type. 我必须将其转换为已知的C#类类型。

I can directly use the known type in DBModel class but it's not allowed to use any known types in my project architecture. 我可以直接在DBModel类中使用已知类型,但是在我的项目体系结构中不允许使用任何已知类型。

Example: 例:

static dynamic getEmployees()
{
    SampleDB db = new SampleDB();
    var result = (from x in db.Employes
    select new
    {
        name = x.FirstName + " " + x.LastName,
        department = x.DepartmentId
    });
    return result;
}

My known type: 我的已知类型:

public class Emp
{
    public string name { set; get; }
    public int department { set; get; }
}

Program: 程序:

dynamic d = getEmployees();
List<Emp> result = d.cast<Emp>();
//Other code....

How can I cast the dynamic type to List? 如何将动态类型转换为List? In getEmployees I can't use like getEmployees我不能像

static List<Emp> getEmployees()
{
    SampleDB db = new SampleDB();
    var result = (from x in db.Employes
    select new Emp
    {
        name = x.FirstName + " " + x.LastName,
        department = x.DepartmentId
    }).ToList();

    return result;
}

Can anybody tell me how can I cast the LINQ return type to a known type? 有人可以告诉我如何将LINQ返回类型转换为已知类型吗?

There's no cast method on the dynamic type. dynamic类型没有cast方法。 What you should do is explicitly cast your dynamic to the required type and because in the dynamic method you have an anonimous object you'll have to do an intermediate step. 您应该做的是将动态显式转换为所需的类型,因为在动态方法中您有一个对象,因此您必须执行一个中间步骤。 Something like: 就像是:

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var json = serializer.Serialize(d);
List<Emp> result = serializer.Deserialize<List<Emp>>(json);

Cast<> is an extension method, and those are not directly supported on dynamic objects. Cast<>是扩展方法,动态对象不直接支持这些方法。

You can get around that, by calling the extension method like this: Enumerable.Cast<Emp>(d) , but this still would not work, there is no known cast from an anonymous type to Emp. 您可以通过调用扩展方法来解决此问题: Enumerable.Cast<Emp>(d) ,但这仍然行不通,没有从匿名类型到Emp的已知转换。

You could try something similar with .Select() , that should work. 您可以尝试使用.Select()类似的东西,它应该可以工作。 However the easiest way is a simple for loop 但是,最简单的方法是简单的for循环

dynamic d = getEmployees();
var emps = new List<Emp>();
foreach (var emp in d)
{
    emps.Add(new Emp(){name = emp.name, department = emp.department};
}

if you want to consume the collection on-demand as an IEnumerable, instead of putting it in a List, you could wrap this in a helper method: 如果要按需将集合作为IEnumerable使用,而不是将其放入List中,则可以将其包装在helper方法中:

IEnumerable<Emp> getEmps(dynamic d)
{
    foreach (var emp in d)
    {
        yield return new Emp(){name = emp.name, department = emp.department};
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM