多个表中加入LINQ的C#动态 [英] Multiple Table Join in Linq C# Dynamically
问题描述
我有3个数据表: A
; B
;和 C
。在此我需要写连接查询动态使用的 LINQ
I have 3 data tables: a
; b
; and c
. In this I need to write Join Query Dynamically using LINQ.
由客户和条件列给出的选择列也给予客户在运行时。
The Selecting columns given by customer and Condition columns also given customer at run time.
所以我需要动态地创建查询。请检查下面的例子。因为我不知道他们要和列还
So I need to create queries dynamically. Please check below example. Because I don't know which table they want and which column also
例如
Select a.c1,a.c2,b.c1,b.c2 From a Left Join b on a.c1=b.c1
Select c.c1,c.c2,a.c1,a.c2 From c Left Join a on c.c3=a.c1
Select a.c1,a.c2,b.c1,b.c2,c.c1,c.c2 From a Left Join b on a.c2=b.c2 Left join c on c.c1=a.c1
$ C b $ b
就像我需要创建不同的组查询。请帮我在这。
Like I need create different set of queries. Please help me on this.
推荐答案
您可以使用任何 System.Linq.Dynamic
( ScottGu的博客文章和的NuGet )动态where子句的情况:
You could use either System.Linq.Dynamic
(ScottGu's blog article and nuget) in case of dynamic where clause:
var results = (from fruit in fruits
join car in cars on fruit.Id equals car.Id
select new { fruit, car })
.AsQueryable()
.Where("fruit.ColA != car.ColA")
.Where("fruit.ColB == car.ColB");
或者dynamicaly构建表达式使用这个的从 PredicateBuilder
通过@乔 - 阿尔巴哈利writen扩展。例如:
Or dynamicaly build expressions this using extensions from PredicateBuilder
writen by @joe-albahari. For example:
var predicate =
PredicateBuilder
.True<Tuple<Product, Product>>()
.And(t => t.Item1.ColA != t.Item2.ColA)
.And(t => t.Item1.ColB == t.Item2.ColB)
.Compile();
(from fruit in fruits
join car in cars on fruit.Id equals car.Id
select Tuple.Create(fruit, car))
.Where(predicate)
.Dump();
PS:在的 gisthub
这篇关于多个表中加入LINQ的C#动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!