如何执行功能?哪种类型的参数应该? [英] How to execute function? which type of paramter should?

查看:54
本文介绍了如何执行功能?哪种类型的参数应该?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用以下功能

通用功能:

how to use following function

Generic Function:

public T GetSingle(Expression<Func<T, bool>> whereCondition)
       {
           return this.ObjectSet.Where(whereCondition).FirstOrDefault<T>();
       }


功能明智:

//现在在以下函数中,我想调用泛型函数.


Function wise:

//Now in the following function i would like to call Generic function.

public TabMasterViewModel GetSingle(Expression<Func<TabMasterViewModel, bool>> whereCondition)
       {

           _tabmasterRepository.GetSingle( .. what should be here.. );
       }



//从控制器级别调用函数.



//Calling function from Controller level.

public ActionResult Details(int id)
       {
           return View(_tabmasterService.GetSingle(x => x.colID == id));
       }




我无法使用该功能,请提出建议.




I could not able to use the function, please suggest.

_tabmasterRepository.GetSingle( .. what should be here.. );



谢谢,
Imdadhusen



Thanks,
Imdadhusen

推荐答案

不幸的是,方法GetSingle是您未显示的某些类的方法.您也没有提供编译此代码所需的其他声明.因此,我们只能推断这是具有至少一个通用参数— T.
的类.
因此,这应该是具有以下内容的类:

The method GetSingle is a method of some class that you did not show, unfortunately. You also did not provide other declarations needed to compile this code. So, we can only infer that this is a class with at least one generic parameter — T.

So, this should be a class with something like this:

class ExpressionTest<T> {
    public T GetSingle(Expression<Func<T, bool>> whereCondition) {
        return this.ObjectSet.Where(whereCondition).FirstOrDefault<T>();
    }
}



现在,在泛型实例化的时刻,我们需要用一个真正的完整类型替换T.对于int类型,它将如下所示:



Now, at the moment of generic instantiation we need to substitute a real complete type for T. For the type int, it will look like this:

ExpressionTest<int> test = new ExpressionTest<int>();



要调用GetSingle,我们需要基于该函数构建一些表达式,该函数接受一个int参数并返回bool:



To call GetSingle we need to build some expression based on the function which accepts one int parameter and returns bool:

using System;
using System.Linq.Expressions;

var test = new ExpressionTest<int>();
Expression<Func<int, bool>> expr = i => i < 5;
test.GetSingle(expr);



要了解这一点,您首先需要更全面地了解泛型.

第二步是理解 lambda表达式,请参见 http://en.wikipedia.org/wiki /Lambda_(编程) [^ ] .有关.NET中的lambda,请参见 http://msdn.microsoft.com/en-us/library/bb397687 .aspx [^ ] , http://msdn.microsoft.com/en-us/magazine/cc163362.aspx [ ^ ].

System.Linq.Expressions中的表达式是更高级的主题.
请参阅 http://msdn.microsoft.com/en-us/library/system .linq.expressions.expression.aspx [ ^ ], http://msdn.microsoft .com/en-us/library/system.linq.expressions.aspx [ ^ ].

阅读介绍: http://msdn.microsoft.com/en-us/library/bb397951.aspx [ ^ ].

为了给出全部含义,我只能给出一个提示:表达式树尤其是可以帮助创建用于.NET中符号数学计算的计算机代数系统(CAS)的基本机制,请参见 http://en.wikipedia.org/wiki/Computer_Algebra_System [



To understand this, you first need to understand generics more thoroughly.

Second step is understanding of lambda expressions, see http://en.wikipedia.org/wiki/Lambda_(programming)[^]. For lambda in .NET, see http://msdn.microsoft.com/en-us/library/bb397687.aspx[^], http://msdn.microsoft.com/en-us/magazine/cc163362.aspx[^].

Expressions in System.Linq.Expressions is much more advanced topic.
See http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.aspx[^], http://msdn.microsoft.com/en-us/library/system.linq.expressions.aspx[^].

Read introduction: http://msdn.microsoft.com/en-us/library/bb397951.aspx[^].

To give an what is it all about, I can give just one hint: Expression trees, in particular, is the base mechanism which can help to create a Computer Algebra System (CAS) for symbolic mathematical calculation in .NET, see http://en.wikipedia.org/wiki/Computer_Algebra_System[^].

—SA


实现功能的简单程序.........

A Simple Program to implement the Functions.........

public partial class _Default : 

System.Web.UI.Page
{
    int add(int x, int y)
    {
        return x + y;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Sum:"+add(75, 100).ToString();
    }
}


这篇关于如何执行功能?哪种类型的参数应该?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆