Click here to Skip to main content
16,021,172 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
how to use following function

Generic Function:
C#
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.
C#
public TabMasterViewModel GetSingle(Expression<Func<TabMasterViewModel, bool>> whereCondition)
       {

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


//Calling function from Controller level.
SQL
public ActionResult Details(int id)
       {
           return View(_tabmasterService.GetSingle(x => x.colID == id));
       }



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

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


Thanks,
Imdadhusen
Posted
Updated 3-Jun-11 23:01pm
v2

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:

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


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:

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


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

C#
using System;
using System.Linq.Expressions;

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


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
 
Share this answer
 
Comments
Sandeep Mewara 5-Jun-11 4:20am    
My 5+!
Sergey Alexandrovich Kryukov 5-Jun-11 12:46pm    
Thank you, Sandeep,
--SA
Espen Harlinn 5-Jun-11 5:35am    
Good effort, my 5
Sergey Alexandrovich Kryukov 5-Jun-11 12:48pm    
Thank you, Espen.
--SA
Sunasara Imdadhusen 6-Jun-11 0:49am    
Thank you very much.
A Simple Program to implement the Functions.........

C#
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();
    }
}
 
Share this answer
 
Comments
Sunasara Imdadhusen 4-Jun-11 5:47am    
cal you please elaborate more... i am not clear right now!
Sergey Alexandrovich Kryukov 5-Jun-11 0:28am    
Nothing to elaborate here. This code has nothing to do with your question. I hope this is just misplaced post.

@lovejeet0707: please removed it. What question does it belong?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900