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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-13 19:58