匿名类型 使用泛型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); } } }