匿名类型 使用泛型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 ...
随机推荐
- 【二分图匹配】BZOJ1562-[NOI2009] 变换序列
[题目大意] 对于0,1,…,N-1的N个整数,给定一个距离序列D0,D1,…,DN-1,定义一个变换序列T0,T1,…,TN-1使得每个i,Ti的环上距离等于Di.一个合法的变换序列应是0,1,…, ...
- Problem A: 逆序输出数列
#include<stdio.h> int main(void) { int n,i,a[100]; while(scanf("%d ",&n)!=EOF) { ...
- mysql远程表/视图-应用
Date :20140213Auth: Jin参考http://blog.sina.com.cn/s/blog_757b0e130101erl5.htmlhttp://dev.mysql.com/do ...
- WPF的UI虚拟化
许多时候,我们的界面上会呈现大量的数据,如包含数千条记录的表格或包含数百张照片的相册.由于呈现UI是一件开销比较大的动作,一次性呈现数百张照片就目前的电脑性能来说是需要占用大量内存和时间的.因此需要对 ...
- 《SQL Server企业级平台管理实践》读书笔记
http://www.cnblogs.com/zhijianliutang/category/277162.html
- List 中的最大最小值
以下实例演示了如何使用 Collections 类的 max() 和 min() 方法来获取List中最大最小值: /* author by w3cschool.cc Main.java */ imp ...
- Java操作xml文件的jar包dom4j
只能解析xml文件,不能解析普通的文件 https://www.cnblogs.com/sharpest/p/7877501.html
- 实现同时提交多个form(基础方法) 收集
实现同时提交多个form(基础方法) 收集 分类: 1.2-JSP 1.3-J2EE 1.1J2se 1.0-Java相关2011-12-01 20:59 1644人阅读 评论(0) 收藏 举报 bu ...
- 二十四种设计模式:解释器模式(Interpreter Pattern)
解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...
- 理解PHP的变量,值与引用的关系
--- title: 理解PHP的变量,值与引用的关系 createdDate: 2015-03-11 category: php --- PHP的变量与C++中的变量是两种截然不容的概念.如果没有理 ...