问题:我要获得一个角色下对应的所有用户,需要两表连接查询,虽然返回的只有用户数据,但是我想到若是返回的不只是用户数据,而还要加上角色信息,那么我返回什么类型呢,返回var吗,这样不行。

于是我网上找找是否能返回DataTable呢,这样我不用创建中间类了。然后就找到下面的代码:这是别人写的,高手。

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace Common
{
public static class DataSetLinqOperators
{
public static DataTable CopyToDataTable<T>(this IEnumerable<T> source)
{
return new ObjectShredder<T>().Shred(source, null, null);
} public static DataTable CopyToDataTable<T>(this IEnumerable<T> source,DataTable table, LoadOption? options)
{
return new ObjectShredder<T>().Shred(source, table, options);
} } public class ObjectShredder<T>
{
private FieldInfo[] _fi;
private PropertyInfo[] _pi;
private Dictionary<string, int> _ordinalMap;
private Type _type; public ObjectShredder()
{
_type = typeof(T);
_fi = _type.GetFields();
_pi = _type.GetProperties();
_ordinalMap = new Dictionary<string, int>();
} public DataTable Shred(IEnumerable<T> source, DataTable table, LoadOption? options)
{
if (typeof(T).IsPrimitive)
{
return ShredPrimitive(source, table, options);
} if (table == null)
{
table = new DataTable(typeof(T).Name);
} // now see if need to extend datatable base on the type T + build ordinal map
table = ExtendTable(table, typeof(T)); table.BeginLoadData();
using (IEnumerator<T> e = source.GetEnumerator())
{
while (e.MoveNext())
{
if (options != null)
{
table.LoadDataRow(ShredObject(table, e.Current), (LoadOption)options);
}
else
{
table.LoadDataRow(ShredObject(table, e.Current), true);
}
}
}
table.EndLoadData();
return table;
} public DataTable ShredPrimitive(IEnumerable<T> source, DataTable table, LoadOption? options)
{
if (table == null)
{
table = new DataTable(typeof(T).Name);
} if (!table.Columns.Contains("Value"))
{
table.Columns.Add("Value", typeof(T));
} table.BeginLoadData();
using (IEnumerator<T> e = source.GetEnumerator())
{
Object[] values = new object[table.Columns.Count];
while (e.MoveNext())
{
values[table.Columns["Value"].Ordinal] = e.Current; if (options != null)
{
table.LoadDataRow(values, (LoadOption)options);
}
else
{
table.LoadDataRow(values, true);
}
}
}
table.EndLoadData();
return table;
} public DataTable ExtendTable(DataTable table, Type type)
{
// value is type derived from T, may need to extend table.
foreach (FieldInfo f in type.GetFields())
{
if (!_ordinalMap.ContainsKey(f.Name))
{
DataColumn dc = table.Columns.Contains(f.Name) ? table.Columns[f.Name]
: table.Columns.Add(f.Name, f.FieldType);
_ordinalMap.Add(f.Name, dc.Ordinal);
}
}
foreach (PropertyInfo p in type.GetProperties())
{
if (!_ordinalMap.ContainsKey(p.Name))
{
DataColumn dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name]
: table.Columns.Add(p.Name, p.PropertyType);
_ordinalMap.Add(p.Name, dc.Ordinal);
}
}
return table;
} public object[] ShredObject(DataTable table, T instance)
{ FieldInfo[] fi = _fi;
PropertyInfo[] pi = _pi; if (instance.GetType() != typeof(T))
{
ExtendTable(table, instance.GetType());
fi = instance.GetType().GetFields();
pi = instance.GetType().GetProperties();
} Object[] values = new object[table.Columns.Count];
foreach (FieldInfo f in fi)
{
values[_ordinalMap[f.Name]] = f.GetValue(instance);
} foreach (PropertyInfo p in pi)
{
values[_ordinalMap[p.Name]] = p.GetValue(instance, null);
}
return values;
}
}
}

来源:http://www.cnblogs.com/jaxu/archive/2011/08/02/2125055.html

Jaxu的博客写挺好的,都可以看看。

不过我最后没有用这个方法,我新建了一个中间类,来形成List返回:

/// <summary>
/// 根据RoleID获得该角色下的用户成员
/// </summary>
/// <param name="rid"></param>
/// <returns></returns>
public List<S_ROLE_USER_Query_Dto> GetRoleUser(int rid, string name)
{
IQueryable<S_ROLE_USER_Query_Dto> result = from ru in this.Context.Set<Domain.S_ROLE_USER>()
join u in this.Context.Set<Domain.S_USER>()
on new { ru.U_ID }
equals new { u.U_ID }
where (ru.R_ID == rid && (string.IsNullOrEmpty(name) ? true : (u.U_NAME.Contains(name) || u.U_REALNAME.Contains(name))))
select new S_ROLE_USER_Query_Dto
{
U_ID = ru.U_ID,
U_NAME = u.U_NAME,
U_REALNAME = u.U_REALNAME,
U_EMAIL = u.U_EMAIL,
U_MOBILE = u.U_MOBILE,
U_TEL = u.U_TEL,
R_ID = ru.R_ID
};
return result.ToList();
}

  

最新文章

  1. iOS 之UITextFiled/UITextView小结
  2. 安装lnmp一键安装包(转)
  3. Ajax代码简单封装。
  4. Adb connection Error:远程主机强迫关闭了一个现有的连接
  5. c++中级 STL基础学习(二)
  6. android 单选、多选弹出菜单
  7. [BZOJ 3622]已经没有什么好害怕的了
  8. 玄机论坛Socket类库源码 当前版本 2.6.3 更新日期:10-09/2015 z
  9. linux命令——Grep 命令 用法大全
  10. JSON解析之Gson
  11. 重构手法之Split Temporary Variable(分解临时变量)
  12. js的一些点
  13. [Swift]LeetCode869. 重新排序得到 2 的幂 | Reordered Power of 2
  14. 用abp vNext快速开发Quartz.NET定时任务管理界面
  15. github总结(4)--关于git reset --hard这个命令的惨痛教训
  16. 备忘:EBS参考链接
  17. 细说ASP.NET Windows身份认证
  18. 03-01 Java运算符
  19. 读书笔记(chapter1-2)
  20. IEnumerable与IEnumerator

热门文章

  1. OpenCV:初试牛刀-显示图像-1
  2. Docker集群管理(一)—— 基础docker+swarm+shipyard
  3. 深度强化学习——连续动作控制DDPG、NAF
  4. 开发组件:tmpfs
  5. WinRAR 代码执行漏洞复现
  6. three3D地图设置两点之间的连线
  7. HTML5 Canvas ( 图形的透明度和遮盖 ) globalAlpha, globalCompositeOperation
  8. Delphi 解析HTML
  9. Activity服务类-5 IdentityService服务类
  10. jquery formValidator 表单验证插件, ajax无法传值到后台问题的解决