我有2个表,需要加入它们才能获得所需的结果。我尝试了多种类型的连接,但没有运气。 请帮忙。我的表格如下:
从t1中选择*
public static class QueryableUtils
{
static Expression<Func<T, TResult>> Expr<T, TResult>(Expression<Func<T, TResult>> source) { return source; }
static MethodInfo GetMethod(this LambdaExpression source) { return ((MethodCallExpression)source.Body).Method; }
static readonly MethodInfo Object_ToString = Expr((object x) => x.ToString()).GetMethod();
static readonly MethodInfo String_Contains = Expr((string x) => x.Contains("y")).GetMethod();
public static IQueryable<T> Filter<T>(this IQueryable<T> query, List<SearchFilterDto> filters)
// where T : BaseEntity
{
if (filters != null && filters.Count > 0 && !filters.Any(f => string.IsNullOrEmpty(f.Filter)))
{
var item = Expression.Parameter(query.ElementType, "item");
var body = filters.Select(f =>
{
// Process the member path and build the final value selector
Expression value = item;
foreach (var memberName in f.Column.Split('.'))
{
var member = item.Type.GetProperty(memberName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) ??
(MemberInfo)item.Type.GetField(memberName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
if (member == null) return null; // Should probably throw an error?
value = Expression.MakeMemberAccess(value, member);
}
// NOTE: "Safe" skipping invalid arguments is not a good practice.
// Without that requirement, the above block will be simply
// var value = f.Column.Split('.').Aggregate((Expression)item, Expression.PropertyOrField);
// Convert value to string if needed
if (value.Type != typeof(string))
{
// Here you can use different conversions based on the value.Type
// I'll just use object.ToString()
value = Expression.Call(value, Object_ToString);
}
// Finally build and return a call to string.Contains method
return (Expression)Expression.Call(value, String_Contains, Expression.Constant(f.Filter));
})
.Where(r => r != null)
.Aggregate(Expression.AndAlso);
var predicate = Expression.Lambda<Func<T, bool>>(body, item);
query = query.Where(predicate);
}
return query;
}
}
从t2中选择*
ProductId, Sequence, Property
100, 1, Size
100, 2, Folder
100, 3, License
101, 1, Usage
101, 2, Duration
我需要加入他们才能获得以下结果:
SrNo, ProductId, Property, PropertyValue
1, 100, Size, 10GB
2, 100, Folder, /home/path
3, 101, Usage, Database
以下是重现相同表格的SQL:
SrNo, ProductId, Sequence, Property, PropertyValue
1 100 1 Size 10GB
1 100 2 Folder
1 100 3 License
2 100 1 Size
2 100 2 Folder /home/path
2 100 3 License
3 101 1 Usage Database
3 101 2 Duration
请协助我如何撰写查询?
答案 0 :(得分:0)
SELECT
table2.SrNo,
table1.ProductId,
table1.Sequence,
table1.Property,
table2.PropertyValue
FROM t1 AS table1
JOIN t2 AS table2 ON table1.ProductId = table2.ProductId
答案 1 :(得分:0)
你去吧
OUTER JOINS
&#34;问题&#34;这是你将T2视为两个不同的表格。当我第一次从T2中选择时,我只选择SrNo和ProductId来获取这些&#34;有效&#34;的列表。值。然后我加入T1。然后我加入T2,将其视为PropertyValue的查找表。这里我使用左连接,因为并非所有Property和Sequence的组合都有一个有效的属性。
我&#34;修复&#34;上面的代码基于评论,但问题变得清晰。在您的示例中显示
SELECT T2.SrNo, T2.ProductId, T1.Sequence, T1.Property, T3.PropertyValue
FROM T2
JOIN T1 ON T2.ProductID = T1.ProductID
LEFT JOIN T2 AS T3 ON T1.Property = T3.Property
我不知道为什么第二行没有10GB的PropertyValue