我有一个用户控件,它接受一个Func,然后它给出了IQueryable的Linq"Where"扩展方法.我的想法是,从调用代码,我可以传递所需的搜索功能.
我想动态地构建这个搜索功能:
Func<Order, bool> func == a => true;
if (txtName.Text.Length > 0) {
//add it to the function
func = a => func(a) && a.Name.StartsWith(txtName.Text);
}
if (txtType.Text.Length > 0) {
//add it to the function
func = a => func(a) && a.Type == txtType.Text;
}
..... etc .....
Run Code Online (Sandbox Code Playgroud)
这种方法的问题在于,因为我重用名称"func",所以它创建了一个递归函数.
是否有一种简单的方法来构建像这样的表达式树来创建一个动态的where子句(在没有IQueryable前面并反复调用"Where")的情况下?
这是一个界面:
public interface Foo<T> extends Comparable<Foo<T>> {
...
}
Run Code Online (Sandbox Code Playgroud)
并且有一些类实现此接口:
public class Bar extends Something implements Foo<Something> {
public Vector<Foo<Bar>> giveBar() {
...
}
}
public class Boo extends SomethingElse implements Foo<SomethingElse> {
public Vector<Foo<Boo>> giveBoo() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想在矢量中保留一堆Foos(可能真的是Foos或Boos).
Bar bar = new Bar();
Boo boo = new Boo();
Vector<Foo<?>> vector;
if (...)
vector = bar.giveBar();
else
vector = boo.giveBoo();
Run Code Online (Sandbox Code Playgroud)
我明白了:
Type mismatch: cannot convert from Vector<Foo<SomethingElse>> to Vector<Foo<?>>
Run Code Online (Sandbox Code Playgroud)
同样适用于:
Vector<Foo> vector;
if (...)
vector = giveBar();
else …Run Code Online (Sandbox Code Playgroud) 给出一张表:
create table #orders (
orderid int,
orderdatetime datetime
)
Run Code Online (Sandbox Code Playgroud)
编写sql以输出包含当前和之前24小时的订单数量,当前和前7天的总订单,当前周和前4周的总订单以及总订单的报告的最佳方法是什么?本月和前6个月?
我想知道是否可以使用分析函数有效地将其汇总到单个sql中,或者如果生成4组数据的4个sql语句是唯一(或最佳)方式.
另外,考虑到每小时/每天/每周分组,如何在sql server中执行此操作?每当我不得不与他们做这样的事情时,日期时间似乎是屁股的痛苦......
想法?放入SSAS立方体并从那里做到吗?
鉴于这种:
Interface IBase {string X {get;set;}}
Interface ISuper {string Y {get;set;}}
class Base : IBase {etc...}
class Super : Base, ISuper {etc...}
void Questionable (Base b) {
Console.WriteLine ("The class supports the following interfaces... ")
// The Magic Happens Here
}
Run Code Online (Sandbox Code Playgroud)
有什么可以替换"魔术"来显示对象b上支持的接口?
是的,我知道作为类Base它支持"IBase",真正的层次结构更加复杂.:)
谢谢!-DF5
编辑:现在我已经看到了答案,我感到愚蠢的是没有通过Intellisense绊倒它.:)
谢谢大家!-DF5
我有一个看起来像这样的表:
ProductId, Color
"1", "red, blue, green"
"2", null
"3", "purple, green"
Run Code Online (Sandbox Code Playgroud)
我想把它扩展到这个:
ProductId, Color
1, red
1, blue
1, green
2, null
3, purple
3, green
Run Code Online (Sandbox Code Playgroud)
什么是最简单的方法来实现这一目标?是否可以在proc中没有循环?
为什么这个WSDL文件在VS2008中生成一个空的服务代理?
如果查看生成的Reference.cs文件,则为空.有任何想法吗?
我在运行时为给定的成员生成编译的getter方法.现在,我的代码只是假设getter方法的结果是一个字符串(适用于测试).但是,我想用我编写的自定义转换器类来完成这项工作,请参阅下面的"ConverterBase"参考,我已经添加了.
我无法弄清楚如何将转换器类的调用添加到我的表达式树.
public Func<U, string> GetGetter<U>(MemberInfo info)
{
Type t = null;
if (info is PropertyInfo)
{
t = ((PropertyInfo)info).PropertyType;
}
else if (info is FieldInfo)
{
t = ((FieldInfo)info).FieldType;
}
else
{
throw new Exception("Unknown member type");
}
//TODO, replace with ability to specify in custom attribute
ConverterBase typeConverter = new ConverterBase();
ParameterExpression target = Expression.Parameter(typeof(U), "target");
MemberExpression memberAccess = Expression.MakeMemberAccess(target, info);
//TODO here, make the expression call "typeConverter.FieldToString(fieldValue)"
LambdaExpression getter = Expression.Lambda(memberAccess, target);
return (Func<U, string>)getter.Compile();
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找第二个TODO区域的内容(我可以处理第一个:)). …
以下代码编译:
class Testing<TKey, TValue>
{
public bool Test(TKey key)
{
return key == null;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,TKey可以是值类型,并且可能不允许值"null".
我知道这个程序的结果,以及如何添加约束.我想知道为什么当TKey不被约束为"类"时,编译器不会禁止这个?
c# ×5
.net ×2
generics ×2
lambda ×2
sql ×2
sql-server ×2
interface ×1
java ×1
linq ×1
linq-to-sql ×1
mismatch ×1
nullable ×1
reflection ×1
report ×1
types ×1
web-services ×1
wsdl ×1