匿名类型 使用泛型T linq返回dynamic类型的匿名实体 如何把匿名类型.GetType()返回的对象传进泛型里面 EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题
[100分]紧急求助:LinQ下使用IQueryable<T>如何将返回类型<T>使用匿名类型
问题描述如下:
我有一个方法如下:
public IQueryable DissensionList(Guid guid)
{
var p = from d in db.T_民事调解 where (d.社区Guid == guid) select new {序号=d.ID,d.纠纷甲方,d.纠纷乙方,d.调解人,d.调解时间,d.纠纷类别 };
return p;
}
现因为需求改变,需要在返回的IQueryable中使用Linq进行再次查询、筛选,但现在返回的类别为IQueryable,而IQueryable不能使用LinQ查询,
但是IQueryable<T>可以,但是<T>的类型是由方法内部中的
select new {序号=d.ID,d.纠纷甲方,d.纠纷乙方,d.调解人,d.调解时间,d.纠纷类别 };
来确定的,而且这个类型是不确定的,现疑问如下:
如何将这个返回的<T>类型设为匿名类型,由返回类型来自适应生成。
或者提供其它解决方案,返回方法内部生成的匿名类型相对应的List<T>或IQueryable<T>谢谢!
看了你的帖子,经过3天的努力我帮你总结了两个可行的方案,第一个方案比较简单点。第二个比较难
方案一: 利用自建类型
public IEnumerable<dd> get()
{
IEnumerable<dd> re = from n in gj.zuixinZhaosheng
select new dd { id = n.id, jz = n.jianzhang };
return re;
}
//手动创建对象类型
public class dd
{
public int id { get; set; }
public string jz { get; set; }
}
客户代码、
protected void Button1_Click(object sender, EventArgs e)
{
zuixinZhaoshengbll b = zuixinZhaoshengbll.getinstance();
foreach (var item in b.get())
{
Response.Write(item.id.ToString() + "</br>");
Response.Write(item.jz.ToString() + "</br>");
}
}
方案二: 反射方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace AnonymouseType
{
class AnonymouseSolve
{
//取得ProductID,ProductName,CategoryName
public IEnumerable GetAnonymousResult()
{
NorthwindDataContext db = new NorthwindDataContext();
var result = db.Products.Select(p => new
{
产品ID = p.ProductID,
产品名称 = p.ProductName,
类别名称 = p.Category.CategoryName
});
return result;
}
//取得ProductID,ProductName,CategoryName的前10条记录
public IEnumerable GetTop10Result(IEnumerable _result)
{
var result = _result.Cast<object>();
var set = result.Select(p => new
{ //利用反射取得传入的匿名类型的信息
产品ID = p.GetType().GetProperty("产品ID").GetValue(p, null).ToString(),
产品名称 = p.GetType().GetProperty("产品名称").GetValue(p, null).ToString(),
类别名称 = p.GetType().GetProperty("类别名称").GetValue(p, null).ToString()
}).Take(10);
return set;
}
public IEnumerable GetTop10Result(object IEnumerableOject)
{
var result = (IEnumerable)IEnumerableOject;
return this.GetTop10Result(result);
}
}
}
测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace AnonymouseType
{
class Program
{
static void Main(string[] args)
{
AnonymouseSolve solve = new AnonymouseSolve();
foreach (var item in solve.GetTop10Result(solve.GetAnonymousResult()))
{
Console.WriteLine("-----------------------------------------");
Type itemType = item.GetType();
Console.WriteLine("产品ID:" + itemType.GetProperty("产品ID").GetValue(item, null));
Console.WriteLine("产品名称:" + itemType.GetProperty("产品名称").GetValue(item, null));
Console.WriteLine("类别名称:" + itemType.GetProperty("类别名称").GetValue(item, null));
Console.WriteLine("-----------------------------------------");
Console.WriteLine();
}
var obj = (object)solve.GetAnonymousResult();
foreach (var item in solve.GetTop10Result(obj))
{
Console.WriteLine("-----------------------------------------");
Type itemType = item.GetType();
Console.WriteLine("产品ID:" + itemType.GetProperty("产品ID").GetValue(item, null));
Console.WriteLine("产品名称:" + itemType.GetProperty("产品名称").GetValue(item, null));
Console.WriteLine("类别名称:" + itemType.GetProperty("类别名称").GetValue(item, null));
Console.WriteLine("-----------------------------------------");
Console.WriteLine();
}
}
}
}
linq返回dynamic类型的匿名实体
这样会报错:

void Main()
{
var x=GetSpareInfoByCode();
Console.Write(x.Key);//报错:“object”未包含“Key”的定义
}
public dynamic GetSpareInfoByCode(){
var words =
from word in "The quick brown fox jumps over the lazy dog".Split()
orderby word.ToUpper()
select word;
var duplicates =
from word in words
group word.ToUpper() by word.ToUpper() into g
where g.Count() > 1
select new { g.Key, Count = g.Count() };
return duplicates;
}

使用tolist

void Main()
{
//var x=GetSpareInfoByCode();
IEnumerable<object> y=GetSpareInfoByCode();
//foreach (var item in (IEnumerable<object>)x)
foreach (var item in y)
{
Console.Write(item.GetType().GetProperty("Key").GetValue(item, null));
Console.Write(item.GetType().GetProperty("Count").GetValue(item, null));
}
}
public dynamic GetSpareInfoByCode(){
var words =
from word in "The quick brown fox jumps over the lazy dog".Split()
orderby word.ToUpper()
select word;
var duplicates =
from word in words
group word.ToUpper() by word.ToUpper() into g
where g.Count() > 1
select new { g.Key, Count = g.Count() };
return duplicates.ToList();
}

有木有更简单高效的方式呢
如何把匿名类型.GetType()返回的对象传进泛型里面

//怎么取得匿名类型的Type放到
//泛型T当中??
var 匿名 = new {
A = 0,
B = 1 };
Type t = 匿名.GetType();
//然后下面
var xx = dbContext.Database.SqlQuery<t>("sql");
//就悲剧了
var xx2 = dbContext.Database.SqlQuery<dynamic>("sql");
//xx2有列表,但是都是Object..~~~无法显示真实项,用Profile跟..SQL确实提交了.
求解释

msdn大神的解决办法
看到了所以记录下来
出现那个问题的原因是动态类型上没有 Entity Framework 需要的属性定义,Entity Framework 是通过反射类型上的属性来做映射的。为了解决这个问题,我用到了 Emit 技术,动态产生一个类型,并且动态写入 IL 代码产生属性。 代码如下,测试在 C# 4.0 上通过。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Collections;
using System.Reflection.Emit;
using System.Reflection;
namespace Demo
{
public class Program
{
public static void Main(string[] args)
{
string connectionString = "Server=(local); Integrated Security=true; Database=master";
using (DbContext context = new DbContext(connectionString))
{
TypeBuilder builder = Program.CreateTypeBuilder("MyDynamicAssembly", "MyModule", "MyType");
Program.CreateAutoImplementedProperty(builder, "name", typeof(string));
Program.CreateAutoImplementedProperty(builder, "type", typeof(string));
Program.CreateAutoImplementedProperty(builder, "id", typeof(int));
Type resultType = builder.CreateType();
dynamic queryResult = context.Database.SqlQuery(resultType, "SELECT * FROM sys.sysobjects");
Console.WriteLine("{0,20} {1,4} {2,10}", "Name", "Type", "ID");
foreach (dynamic item in queryResult)
{
Console.WriteLine("{0,10} {1,4} {2,10}", item.name, item.type, item.id);
}
}
Console.ReadKey();
}
public static TypeBuilder CreateTypeBuilder(string assemblyName, string moduleName, string typeName)
{
TypeBuilder typeBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.Run).DefineDynamicModule(moduleName).DefineType(typeName, TypeAttributes.Public);
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
return typeBuilder;
}
public static void CreateAutoImplementedProperty(TypeBuilder builder, string propertyName, Type propertyType)
{
const string PrivateFieldPrefix = "m_";
const string GetterPrefix = "get_";
const string SetterPrefix = "set_";
// Generate the field.
FieldBuilder fieldBuilder = builder.DefineField(string.Concat(PrivateFieldPrefix, propertyName), propertyType, FieldAttributes.Private);
// Generate the property
PropertyBuilder propertyBuilder = builder.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);
// Property getter and setter attributes.
MethodAttributes propertyMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
// Define the getter method.
MethodBuilder getterMethod = builder.DefineMethod(string.Concat(GetterPrefix, propertyName), propertyMethodAttributes, propertyType, Type.EmptyTypes);
// Emit the IL code.
// ldarg.0
// ldfld,_field
// ret
ILGenerator getterILCode = getterMethod.GetILGenerator();
getterILCode.Emit(OpCodes.Ldarg_0);
getterILCode.Emit(OpCodes.Ldfld, fieldBuilder);
getterILCode.Emit(OpCodes.Ret);
// Define the setter method.
MethodBuilder setterMethod = builder.DefineMethod(string.Concat(SetterPrefix, propertyName), propertyMethodAttributes, null, new Type[] { propertyType });
// Emit the IL code.
// ldarg.0
// ldarg.1
// stfld,_field
// ret
ILGenerator setterILCode = setterMethod.GetILGenerator();
setterILCode.Emit(OpCodes.Ldarg_0);
setterILCode.Emit(OpCodes.Ldarg_1);
setterILCode.Emit(OpCodes.Stfld, fieldBuilder);
setterILCode.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getterMethod);
propertyBuilder.SetSetMethod(setterMethod);
}
}
}

EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题

public static DataTable ToDataTable<T>(this IEnumerable<T> varlist, CreateRowDelegate<T> fn)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
// Could add a check to verify that there is an element 0
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
}
dtReturn.Rows.Add(dr);
}
return (dtReturn);
}
public delegate object[] CreateRowDelegate<T>(T t);

List<T> list = DAL.ToList();
dataGridView1.DataSource = list.ToDataTable(a => new object[] { list });
以上代码可解决
匿名类型 使用泛型T linq返回dynamic类型的匿名实体 如何把匿名类型.GetType()返回的对象传进泛型里面 EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题的更多相关文章
- 完美解决方案,可排除DATASET不支持System.Nullable错误
完美解决方案,可排除DATASET不支持System.Nullable错误 using System; using System.Collections.Generic; using System.L ...
- EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题
public static DataTable ToDataTable<T>(this IEnumerable<T> varlist, CreateRowDelegate< ...
- 如何把匿名类型.GetType()返回的对象传进泛型里面[转]
//怎么取得匿名类型的Type放到 //泛型T当中?? var 匿名 = new { A = 0, B = 1 }; Type t = 匿名.GetType(); //然后下面 var xx = db ...
- 将JDBC查询出的数据转化为json格式返回
使用JDBC,json工具使用的org.json /** * ResultSet转JSON * * @param rs * @return * @throws SQLException * @thro ...
- Java基于POI实现excel任意多级联动下拉列表——支持从数据库查询出多级数据后直接生成【附源码】
Excel相关知识点 (1)名称管理器--Name Manager [CoderBaby]首先需要创建多个名称(包含key及value),作为下拉列表的数据源,后续通过名称引用.可通过菜单:&quo ...
- 无法将类型“System.Nullable`1”强制转换为类型“System.Object”。LINQ to Entities 仅支持强制转换 EDM 基元或枚举类型。
在一个项目中使用LINQ和EF时出现了题目所示的异常,搜索了很多资料都找不到解决办法,主要是因为EF方面的知识欠缺. 先将情况记录如下,以供以后参考. 查询主要设计两张表,由外键关联: 在进行下面的查 ...
- C#3.0新特性:隐式类型、扩展方法、自动实现属性,对象/集合初始值设定、匿名类型、Lambda,Linq,表达式树、可选参数与命名参数
一.隐式类型var 从 Visual C# 3.0 开始,在方法范围中声明的变量可以具有隐式类型var.隐式类型可以替代任何类型,编译器自动推断类型. 1.var类型的局部变量必须赋予初始值,包括匿名 ...
- Dll中的方法向外返回dynamic类型可能会失败
如果Dll中有某个类的方法返回dynamic实例,并且dynamic对象实际实例为匿名类类型,则Dll的外部使用者可能最终无法正常使用此dynamic对象.当使用此dynamic对象时,可能会遇到x属 ...
- allow zero datetime=true导致datetime转换失败:MySql.Data.Types.MySqlDateTime”的对象无法转换为类型“System.Nullable`1[System.DateTime]
allow zero datetime=true导致datetime转换失败:MySql.Data.Types.MySqlDateTime”的对象无法转换为类型“System.Nullable`1[S ...
随机推荐
- vue组件续和前端工程化
1.3 插槽 slot template: ` <button> <slot></slot> </button> ` <my-button> ...
- [转]spring4.x注解概述
1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能,因项目中用到不少注解,因此下定决心,经spring4.x中涉及到的注解罗列出来,供查询使用. 2. spring注解图 ...
- iOS 未读消息角标 仿QQ拖拽 简单灵活 支持xib(源码)
一.效果 二.简单用法 超级简单,2行代码集成:xib可0代码集成,只需拖一个view关联LFBadge类即可 //一般view上加角标 _badge1 = [[LFBadge alloc] init ...
- 谷歌地图聚合点使用(GoogleMaps MarkerCluster)
我们有时候需要观察地图 不同地方数据的所在范围和分布密集情况,热力图和聚合点的使用无疑是最好的选择. 1.首先说说百度地图,只做国内的地图可以使用百度地图的海量点和热力图还是蛮好用的. a.海量点的最 ...
- Web API使用记录系列(一)创建API项目与基本配置
本系列文章主要记录Web API使用过程中的一些个人总结,包括创建API项目.基础配置.ApiTestClient使用与HelpPage页面的优化.Owin与OAuth的使用等. 本节主要内容是API ...
- Struct2_使用Ajax调用Action方法并返回值
一.Login.jsp 1.<head>引入jquery: <script type="text/javascript" src="http://aja ...
- BOOST 实用手册(摘录自校友博客)
1. 序言 现在学的东西很容易忘记,写这篇文章的目的是能让我在需要时快速找回当时的感觉. Let's BOOST THE WORLD . 2. 编译:VC2005注意 在 属性->C/C++- ...
- 客户端Git的常用命令
(1)git clone 服务器用户名@服务器IP:~/Git目录/.git 功能:下载服务器端Git仓库中的文件或目录到本地当前目录. (2)git status 功能:查看Git仓库中的文件状态. ...
- CentOS 下 LNMP 环境配置
安装配置 Nginx 安装配置 MySQL 安装配置 PHP Nginx 与 PHP-FPM 集成 环境配置验证 LNMP 环境代表 Linux 系统下 Nginx + MySQL + PHP 网 ...
- Newtonsoft.Json.4.5.11使用方法总结---反序列化json字符串
写在开头: 最近项目需求,需要在C#中处理json字符串,毫不犹豫的下载了Newtonsoft.Json 4.5.11(2012.12.17)http://json.codeplex.com/,然后百 ...