如何从使用 LINQ to SQL 的 c# 方法返回匿名类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1070526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return anonymous type from c# method that uses LINQ to SQL
提问by alchemical
Possible Duplicate:
LINQ to SQL: Return anonymous type?
可能的重复:
LINQ to SQL:返回匿名类型?
I have a standard LINQ to SQL query, which returns the data as an anonymous type (containing about 6 columns of data of various datatypes).
我有一个标准的 LINQ to SQL 查询,它以匿名类型返回数据(包含大约 6 列各种数据类型的数据)。
I would like to make this returned object available to other parts of the program, either by returning it to the method-caller, or by assigning it to a property of the object containing the method.
我想让这个返回的对象可用于程序的其他部分,或者通过将它返回给方法调用者,或者通过将它分配给包含该方法的对象的属性。
How can I do this given that it is an anonymous type ("var")?
鉴于它是匿名类型(“var”),我该怎么做?
EDIT - Here is the code:
编辑 - 这是代码:
using (ormDataContext context = new ormDataContext(connStr))
{
var electionInfo = from t1 in context.elections
join t2 in context.election_status
on t1.statusID equals t2.statusID
select new { t1, t2 };
}
采纳答案by alchemical
Make the anonymous type into a class...
将匿名类型变成一个类...
public class Person
{
public Person() {
}
public String Name { get; set; }
public DateTime DOB { get; set; }
}
Person p =
from person in db.People
where person.Id = 1
select new Person {
Name = person.Name,
DOB = person.DateOfBirth
}
回答by JaredPar
You cannot type any method in C# to be the explicit type of an anonymous types. They cannot be "named" so to speak and hence cannot appear in metadata signatures.
您不能在 C# 中将任何方法键入为匿名类型的显式类型。它们不能被“命名”,因此不能出现在元数据签名中。
If you really want to return a value which is an anonymous type there are 2 options
如果你真的想返回一个匿名类型的值,有 2 个选项
- Have the return type of the method be System.Object. You can then do evil casting hacks to get a typed value in another method. This is veryfragile and I don't recommend it.
- Use a generic method and a type inference trick to get the return type correct. This would require a very interesting signature definition for your approach.
- 方法的返回类型为 System.Object。然后,您可以执行邪恶的转换技巧以在另一种方法中获取类型化的值。这是非常脆弱的,我不推荐它。
- 使用泛型方法和类型推断技巧来获得正确的返回类型。这将需要为您的方法提供一个非常有趣的签名定义。
Anonymous types were not really meant to be passed around in this fashion. At the point you need to pass them around between your functions in this manner, you're better off explicitly defining a type.
匿名类型并不是真的要以这种方式传递。当您需要以这种方式在函数之间传递它们时,最好明确定义类型。
回答by Jeff Yates
Jon Skeet wrote a blog on how to do this, which is quite rightly titled Horribly Grotty Hack. Just as the title suggests, you really shouldn't be looking for ways to return an anonymous type. Instead, you should be creating a type that can be returned as this is the correct way to implement this feature.
Jon Skeet 写了一篇关于如何做到这一点的博客,它的标题是Horribly Grotty Hack。正如标题所暗示的那样,您真的不应该寻找返回匿名类型的方法。相反,您应该创建一个可以返回的类型,因为这是实现此功能的正确方法。
Therefore, I recommend you create a concrete definition of the type to be returned and then populate that in your query for it to be returned.
因此,我建议您创建要返回的类型的具体定义,然后在您的查询中填充它以返回它。
回答by tvanfosson
Using var doesn't make it an anonymous type. Using var just means let this variable be of the type available on the right-hand side of the assignment. It's just short hand. If the thing on the right-hand side is a real class, the variable will be of that type.
使用 var 不会使其成为匿名类型。使用 var 只是意味着让这个变量成为赋值右侧可用的类型。这只是手短。如果右边的东西是一个真正的类,那么变量就是那种类型。
For example:
例如:
var person = new Person { Name = "bob" };
The person variable is of type Person, even though it used the var keyword.
person 变量的类型是 Person,即使它使用了 var 关键字。
Anonymous types are created using new { Name = ... }. In this case it's a new, anonymous class. The only thing you can assign it to is a variable defined using var (or object) since there is no existing name to use.
匿名类型是使用 new { Name = ... } 创建的。在这种情况下,它是一个新的匿名类。唯一可以分配给它的是使用 var(或 object)定义的变量,因为没有现有名称可供使用。
For example:
例如:
var person = new { Name = "bob" };
In this case, person is an anonymous type defined at run time.
在这种情况下,person 是在运行时定义的匿名类型。
Generally, as @Chalkey says, if you want to pass the result back to another method, use a named type, not an anonymous one.
通常,正如@Chalkey 所说,如果要将结果传递回另一个方法,请使用命名类型,而不是匿名类型。
If you are forced to use an anonymous type, you'll have to pass it back as an object of type Object
, then use reflection to get at it's properties.
如果您被迫使用匿名类型,则必须将其作为 type 对象传回Object
,然后使用反射来获取它的属性。
回答by David Robbins
If you have to pass around results in a large app you can use Dictionary. Yes you have some overhead when casting but at least you are reducing you throw away objects.
如果您必须在大型应用程序中传递结果,您可以使用 Dictionary。是的,您在投射时有一些开销,但至少您减少了扔掉的物体。
回答by Chris Brandsma
It kind of depends on how the calling code is going to use the data.
这在某种程度上取决于调用代码将如何使用数据。
If you are doing simple data binding, are really don't care about the type (i.e. you don't have to explicitly access properties in your C# code), you can pass the results back as an IEnumberable.
如果您正在做简单的数据绑定,真的不关心类型(即您不必显式访问 C# 代码中的属性),您可以将结果作为 IEnumberable 传回。
In most databinding cases you are calling properties by name, via magic strings, anyway, so the exact type doesn't matter anyway.
在大多数数据绑定情况下,无论如何,您都是通过魔术字符串按名称调用属性,因此确切的类型并不重要。
Otherwise, you need to convert the anonymous type to a named type.
否则,您需要将匿名类型转换为命名类型。