使用选择新的内部类文件将linq转换为sql [英] linq to sql using select new inside class file
本文介绍了使用选择新的内部类文件将linq转换为sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通常使用linq来通过 IQueryable 或 IEnumerable 进行sql来获取这样的数据:>
I usually use linq to sql with an IQueryable or IEnumerable to get data like this:
public static IQueryable getSomething()
{
var getit = from d in database.table select d;
return getit;
}
,它工作正常,但我正尝试使用select new来像这样:
and it works fine but i am trying to use select new with it like this:
public static IQueryable getSomething()
{
var getit = from d in database.table select new
{
value1 = d.1,
value2 = d.2
};
return getit;
}
这是示例代码,而不是实际代码.
this is a sample code and not the actual code.
但是那行不通,我该怎么办?
but that wont work, how do i do this?
谢谢
推荐答案
您不能在C#中键入任何方法作为匿名类型的显式类型.可以说,它们不能被命名",因此不能出现在元数据签名中.
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.
https://stackoverflow.com/a/1070564/971839
首先,您需要创建一个类似的类,
First, You need to create a class like,
public class MyClass
{
public string Property1{get;set;}
public string Property2{ get; set; }
}
然后将您的方法替换为
public static IEnumerable<MyClass> getSomething()
{
var getit = from d in database.table select new
MyClass {
Property1 = d.1,
Property2 = d.2
};
return getit;
}
这篇关于使用选择新的内部类文件将linq转换为sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文