匿名类型 使用泛型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-cli创建vue项目
原文出处:https://segmentfault.com/a/1190000008922234 第一步 node环境安装 1.1 如果本机没有安装node运行环境,请下载node 安装包进行安装1. ...
- hdu 4071& poj 3873 & zoj 3386 & uva 12197 Trick or Treat 三分法
思路: 看到这个题目就发现所需最短时间也就是房子和相遇点的最远距离具有凹凸性,很容易就想到了三分法枚举. 找出所有房子的X坐标的最小最大值作为上下界. 代码如下: #include<stdio. ...
- Problem D: 零起点学算法95——弓型矩阵
#include<stdio.h> #include<string.h> int main() { ][]; while(scanf("%d%d",& ...
- 2016.4.3 动态规划NOI专练 王老师讲课整理
1.6049:买书 总时间限制: 1000ms 内存限制: 65536kB 描述 小明手里有n元钱全部用来买书,书的价格为10元,20元,50元,100元. 问小明有多少种买书方案?(每种书可购买 ...
- php 自动补位
1 sprintf("%06d", 2); 2 str_pad();
- linux下的udev是干嘛的,能否说的通俗点
转:http://www.360doc.com/content/11/0415/21/1317564_109924863.shtml 早期的linux的/dev目录下有一大堆设备文件,不管你的主机上是 ...
- appium+python自动化50-生成定位对象模板templet(jinja2)
前言 每次自己写pageobject定位元素对象太繁琐,格式都差不多,只是换个定位方法,这种就可以才有模板的方式,批量生成pageobject定位元素对象的模板 python里面生成模板有两个模块可以 ...
- Ping Pod不通问题定位及Ingress验证
Ping Pod网络问题不通定位记录 1.验证墙是否通 flannel默认使用8285端口作为UDP封装报文的端口,VxLan使用8472端口,下面命令验证一下确定其在8472端口 ip -d lin ...
- [Guava] EventBus
1. 发布-订阅模式 发布-订阅模式(publish-subscribe)是一种编程范式,发布方不发布消息给特定的接收方,而是由订阅方选择性接收.这使得发布方和订阅方相对独立,减少了耦合性. 在发布 ...
- third-maximum-number
https://leetcode.com/problems/third-maximum-number/ // 开始我以为相同的也占一位,比如5,3,3,2,得出3,但是答案是需要2 public cl ...