小编Ric*_*end的帖子

Lambda表达式以及如何将它们组合起来?

如何使用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)

.net c# linq generics lambda

14
推荐指数
1
解决办法
8148
查看次数

在通用抽象类中实现强制转换操作符

我试图变得懒惰并在抽象基类中实现转换运算符,而不是在每个派生的具体类中实现.我已经成功地投了一条路,但是我无法施展另一条路.我认为这可能是不可能的,但是在放弃之前想要选择集体的思想:

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)

c# generics abstract-class operator-keyword

13
推荐指数
1
解决办法
4015
查看次数

Linq对实体的支持是什么?

有谁知道我在哪里可以获得linq到实体的受支持语句的完整列表,这是将被翻译并在数据库上运行的语句......?

.net c# linq linq-to-entities entity-framework

9
推荐指数
1
解决办法
119
查看次数

如何'不'实体框架的lambda表达式

鉴于以下内容

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)

但实体框架不支持这个......

问候

.net c# lambda entity-framework

9
推荐指数
1
解决办法
829
查看次数

WPF WCF Prism和MVVM - 公开实体的正确方法

我们将很快开发一个大型企业桌面应用程序,我花了一些时间研究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吗?

有没有人有更好的解决方案?

.net wpf prism wcf-data-services

7
推荐指数
1
解决办法
1872
查看次数

在Linq中使用Expression <Func <T,X >>包含扩展名

使用下面的示例我想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)

.net c# linq lambda linq-expressions

7
推荐指数
1
解决办法
4616
查看次数

如何在C#中检查userControl是否在其他人面前?

你如何检查userControl是否在他人面前?有没有简单的方法呢?单击用户控件时我使用了bringToFront方法,但现在我需要确定它是否在前面.

.net c# winforms

6
推荐指数
1
解决办法
4315
查看次数

在运行时编译代码,加载到当前appdomain但Type.GetType无法看到它

我在运行时编译一些代码,然后将程序集加载到当前的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)

.net c# asp.net compiler-construction reflection

5
推荐指数
1
解决办法
6063
查看次数

运行时属性的 Web API 条件序列化

我正在考虑在 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)

c# rest asp.net-web-api

5
推荐指数
1
解决办法
5365
查看次数

找出矩形中的长/纬度

给定一个左上角的长/纬度和一个右下角的长/纬度,如何确定给定的长/纬度是否落在矩形内?

理想情况下,我会看到类似的东西

bool IsWithinArea(float topLeftLat,float topLeftLong,
   float bottomRightLat,float bottomRightLong,float testLat,float testLong)
Run Code Online (Sandbox Code Playgroud)

更新

一个问题是从long/lat创建的矩形可能来自旋转的地图,因此右下角并不总是大于左上角...

c# maps geospatial coordinates

3
推荐指数
1
解决办法
2682
查看次数

具有泛型的扩展方法

我正在看这个问题,我很好奇为什么这个不能编译.

鉴于此代码,任何人都可以解释为什么调用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)

.net c# generics extension-methods

2
推荐指数
1
解决办法
1990
查看次数