如何使用OR将两个lambda表达式合并为一个?
我尝试了以下但是合并它们需要我将参数传递给Expression.Invoke调用,但是我想将传递给新lambda的值传递给每个child-lambda.
Expression<Func<int, bool>> func1 = (x) => x > 5;
Expression<Func<int, bool>> func2 = (x) => x < 0;
//Combines the lambdas but result in runtime error saying I need to pass in arguments
//However I want the argument passed into each child lambda to be whatever is passed into the new main lambda
Expression<Func<int, bool>> lambda = Expression.Lambda<Func<int, bool>>(Expression.Or(Expression.Invoke(func1), Expression.Invoke(func2)));
//The 9 should be passed into the new lambda and into both child lambdas
bool tst …Run Code Online (Sandbox Code Playgroud) 我试图变得懒惰并在抽象基类中实现转换运算符,而不是在每个派生的具体类中实现.我已经成功地投了一条路,但是我无法施展另一条路.我认为这可能是不可能的,但是在放弃之前想要选择集体的思想:
public interface IValueType<T>
{
T Value{ get; set; }
}
public abstract class ValueType<T> : IValueType<T> {
public abstract T Value { get; set; }
public static explicit operator T(ValueType<T> vt) {
if(vt == null)
return default(T);
return vt.Value;
}
public static implicit operator ValueType<T>(T val) {
ValueType<T> vt = new ValueType<T>(); //<--- obviously this won't work as its abstract
vt.Value = val;
return vt;
}
}
Run Code Online (Sandbox Code Playgroud) 有谁知道我在哪里可以获得linq到实体的受支持语句的完整列表,这是将被翻译并在数据库上运行的语句......?
鉴于以下内容
Expression<Func<T,bool>> matchExpression;
Run Code Online (Sandbox Code Playgroud)
如何创建另一个与现有表达式"不"的表达式.
我试过了
Expression<Func<T, bool>> func3 = (i) => !matchExpression.Invoke(i);
Run Code Online (Sandbox Code Playgroud)
但实体框架不支持这个......
问候
我们将很快开发一个大型企业桌面应用程序,我花了一些时间研究WPF + PRISM + MVVM方法,我已经很好地掌握了大多数概念,并且非常喜欢它提供的模块化.
我遇到问题的方法是如何构建服务层以获取数据,特别是当模块引入该服务时,依赖模块可以使用它.
我想在应用程序服务中抽象我的WCF数据服务,并用于ServiceLocator从我的视图模型中解析具体实例,但是我很难弄清楚它应该如何挂起,主要是因为我的entites是WCF服务的一部分.
例如
Module1 包含WCF服务+具体应用程序服务(ISearchService)+ WCF服务生成的实体(模型)
Module1.Infastructure - 包含应用程序服务的以下接口
public interface ISearchService
{
ObservableCollection<Person> Search(string search);
}
Run Code Online (Sandbox Code Playgroud)
这将在UnityContainer中注册,以便任何其他模块可以获得模块内嵌的具体实现.
我的问题是Entities(Person)是在模块本身(在WCF服务中)中定义的,因此引入服务然后期望任何其他模块能够使用它意味着他们需要引用模块本身而不仅仅是模块infastructure除非我将服务撤出到另一个程序集中.
我应该以这种方式暴露我的EF模型自动生成的entites吗?
有没有人有更好的解决方案?
使用下面的示例我想Expression在我的Contains方法中使用我的内容,让它使用EF.将查询传递到sql server .
我怎样才能使这个正常工作?
void Main()
{
IQueryable<Person> qry = GetQueryableItemsFromDB();
var filtered = qry.Filter(p=>p.CompanyId);
}
public static class Ext
{
public static IQueryable<T> Filter<T>(this IQueryable<T> items, Expression<Func<T, int>> resolveCompanyIdExpression)
{
IEnumerable<int> validComps = GetCompanyIdsFromDataBase();
var exp = Expression.Lambda<Func<T, bool>>(
Expression.Call(typeof(Queryable),"Contains", new[] { typeof(Company) },
Expression.Constant(validComps),
resolveCompanyIdExpression.Body),
resolveCompanyIdExpression.Parameters[0]);
return items.Where(exp);
}
public static IQueryable<T> Filter<T>(this IQueryable<T> items, Expression<Func<T, IEnumerable<int>>> resolveCompanyIdExpression)
{
IEnumerable<int> validComps = GetCompanyIdsFromDataBase();
//No Idea what to do here ?
}
}
public class …Run Code Online (Sandbox Code Playgroud) 你如何检查userControl是否在他人面前?有没有简单的方法呢?单击用户控件时我使用了bringToFront方法,但现在我需要确定它是否在前面.
我在运行时编译一些代码,然后将程序集加载到当前的appdomain,但是当我尝试做Type.GetType时,它无法找到类型...
以下是我编译代码的方法......
public static Assembly CompileCode(string code)
{
Microsoft.CSharp.CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateExecutable = false;
compilerparams.GenerateInMemory = false;
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
string location = assembly.Location;
if (!String.IsNullOrEmpty(location))
{
compilerparams.ReferencedAssemblies.Add(location);
}
}
catch (NotSupportedException)
{
// this happens for dynamic assemblies, so just ignore it.
}
}
CompilerResults results =
compiler.CompileAssemblyFromSource(compilerparams, code);
if (results.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in …Run Code Online (Sandbox Code Playgroud) 我正在考虑在 ASP.Net 中使用 WebAPI 构建 API。
我需要基于 at和 not 的一些自定义逻辑有条件地从 XML 或 JSON 中排除属性。RunTimeCompile Time
我必须从响应中删除 xml 或 json,仅包含具有 null 或空值的标签是不好的。
我尝试了各种方法,但似乎没有一种方法可以开始工作。
我已经尝试了以下
从这里委托处理程序
public class ResponseDataFilterHandler : DelegatingHandler
{
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
var response = task.Result;
//Manipulate content here
var content = response.Content as ObjectContent;
if (content != null && content.Value != null)
{
}
//Or replace the content
//response.Content = new …Run Code Online (Sandbox Code Playgroud) 给定一个左上角的长/纬度和一个右下角的长/纬度,如何确定给定的长/纬度是否落在矩形内?
理想情况下,我会看到类似的东西
bool IsWithinArea(float topLeftLat,float topLeftLong,
float bottomRightLat,float bottomRightLong,float testLat,float testLong)
Run Code Online (Sandbox Code Playgroud)
更新
一个问题是从long/lat创建的矩形可能来自旋转的地图,因此右下角并不总是大于左上角...
我正在看这个问题,我很好奇为什么这个不能编译.
鉴于此代码,任何人都可以解释为什么调用IBase.Test()不能解析为正确的扩展方法?
public interface IBase { }
public interface IChildA : IBase { }
public interface IChildB : IBase { }
public static class BaseExtensions
{
public static IBase Test(this IBase self) { return self; }
public static T Test<T>(this T self) where T : IChildB { return self; }
}
public static class TestClass
{
public static void Test()
{
IChildA a = null; //Yeh i know its null but just testing for compile here.. …Run Code Online (Sandbox Code Playgroud) c# ×10
.net ×8
generics ×3
lambda ×3
linq ×3
asp.net ×1
coordinates ×1
geospatial ×1
maps ×1
prism ×1
reflection ×1
rest ×1
winforms ×1
wpf ×1