求 动态linq 的写法

jacklikeaccp 2015-09-15 09:21:06
现在在写一个项目,其中有一个功能点,需要用到动态生成linq语句进行数据查询。具体需求如下:
现有两张数据表,表A和表B。表A为数据表,表B为字典表,表结构如下:


现在要检查表A中的各字段值是否在表B的字典值域内.
from p in 表A
where p.字段1

这个where后面的字段值应该是动态从表B中读取的。然后检查这个字段是否在表B中。不知道我表述的是否清楚。
在线等,就37分了,全送出去,大家别嫌少啊。
...全文
172 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mingcsharp 2015-09-15
  • 打赏
  • 举报
回复
感觉像是linq中的左联接么
ajianchina 2015-09-15
  • 打赏
  • 举报
回复
反射

string field = "字段1"; //字段名
var b = 表B.Select(p => p.字典代码);
var query = 表A.Where(p => b.Contains((string)p.GetType().GetProperty(field).GetValue(p, null));
jacklikeaccp 2015-09-15
  • 打赏
  • 举报
回复
引用 2 楼 ajianchina 的回复:
//就四个字段,很简单,无需反射

static IEnumerable<表A_Class> Fun(string field)
{
	var b = 表B.Select(p => p.字典代码);
	switch(field)
	{
		case "字段1":
			return 表A.Where(p => b.Contains(p.字段1));
		case "字段2":
			return 表A.Where(p => b.Contains(p.字段2));
		case "字段3":
			return 表A.Where(p => b.Contains(p.字段3));
		case "字段4":
			return 表A.Where(p => b.Contains(p.字段4));
		default: return null;
	}
}
大哥,我只是给你举个例子
菜鸟也要飞 2015-09-15
  • 打赏
  • 举报
回复
引用 3 楼 liaowubinf 的回复:
from p in 表A join b1 in 表B on p.字段1=b1.字典代码 into temp1 from t1 in temp1.DefaultIfEmpty() join b2 in 表B on t1.字段2=b2.字典代码 into temp2 from t2 in temp2.DefaultIfEmpty() join b3 in 表B on t2.字段3=b3.字典代码 into temp3 from t3 in temp3.DefaultIfEmpty() join b4 in 表B on t3.字段4=b4.字典代码 into temp4 from t4 in temp4.DefaultIfEmpty() select new { id1=p.字段1, name1=b1.字段名, id2=p.字段2, name2=b2.字段名, id3=p.字段3, name3=b3.字段名, id4=p.字段4, name4=b4.字段名 }
from p in 表A join b1 in 表B on p.字段1=b1.字典代码 into temp1 from t1 in temp1.DefaultIfEmpty() join b2 in 表B on p.字段2=b2.字典代码 into temp2 from t2 in temp2.DefaultIfEmpty() join b3 in 表B on p.字段3=b3.字典代码 into temp3 from t3 in temp3.DefaultIfEmpty() join b4 in 表B on p.字段4=b4.字典代码 into temp4 from t4 in temp4.DefaultIfEmpty() select new { id1=p.字段1, name1=b1.字段名, id2=p.字段2, name2=b2.字段名, id3=p.字段3, name3=b3.字段名, id4=p.字段4, name4=b4.字段名 }
菜鸟也要飞 2015-09-15
  • 打赏
  • 举报
回复
from p in 表A join b1 in 表B on p.字段1=b1.字典代码 into temp1 from t1 in temp1.DefaultIfEmpty() join b2 in 表B on t1.字段2=b2.字典代码 into temp2 from t2 in temp2.DefaultIfEmpty() join b3 in 表B on t2.字段3=b3.字典代码 into temp3 from t3 in temp3.DefaultIfEmpty() join b4 in 表B on t3.字段4=b4.字典代码 into temp4 from t4 in temp4.DefaultIfEmpty() select new { id1=p.字段1, name1=b1.字段名, id2=p.字段2, name2=b2.字段名, id3=p.字段3, name3=b3.字段名, id4=p.字段4, name4=b4.字段名 }
ajianchina 2015-09-15
  • 打赏
  • 举报
回复
//就四个字段,很简单,无需反射

static IEnumerable<表A_Class> Fun(string field)
{
	var b = 表B.Select(p => p.字典代码);
	switch(field)
	{
		case "字段1":
			return 表A.Where(p => b.Contains(p.字段1));
		case "字段2":
			return 表A.Where(p => b.Contains(p.字段2));
		case "字段3":
			return 表A.Where(p => b.Contains(p.字段3));
		case "字段4":
			return 表A.Where(p => b.Contains(p.字段4));
		default: return null;
	}
}
本拉灯 2015-09-15
  • 打赏
  • 举报
回复
没有这东西 的写法。你要动态只能自己拼接SQL

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧