Error in Linq and delegate stuff

Posted by Ajayk3 under ASP.NET on 4/2/2014 | Points: 10 | Views : 612 | Status : [Member] | Replies : 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public delegate bool IsValid(EMP objEmp);
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<EMP> obj = new List<EMP>();
obj.Add(new EMP() { Name = "Ajay", Salary = 4000, Experience = 4 });
obj.Add(new EMP() { Name = "Amit", Salary = 4000, Experience = 4 });
obj.Add(new EMP() { Name = "Anil", Salary = 11000, Experience = 4 });

IsValid objIsValid = x => x.Salary > 3000;


List<EMP> list = EMP.IsValidated(obj, objIsValid);

foreach (EMP oEmp in list)
{
Response.Write(oEmp.Name);
}
}
}

public class EMP
{
public string Name { get; set; }
public int Salary { get; set; }
public int Experience { get; set; }

public static List<EMP> IsValidated(List<EMP> objEmp, IsValid del)
{
List<EMP> objK = objEmp.Select(del);
return objK;
}

}


Following error is coming

The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.




Responses

Posted by: Ajay.Kalol on: 4/2/2014 [Member] Starter | Points: 25

Up
0
Down
public delegate bool IsValid(EMP objEmp);
public class TestClass
{
public void test()
{
List<EMP> obj = new List<EMP>();
obj.Add(new EMP() { Name = "Ajay", Salary = 4000, Experience = 4 });
obj.Add(new EMP() { Name = "Amit", Salary = 5000, Experience = 4 });
obj.Add(new EMP() { Name = "Anil", Salary = 11000, Experience = 4 });

Func<EMP, bool> objDel = p => p.Salary < 5000;


List<EMP> list = EMP.IsValidated(obj, objDel);
}
}
public class EMP
{
public string Name { get; set; }
public int Salary { get; set; }
public int Experience { get; set; }

public static List<EMP> IsValidated(List<EMP> objEmp, Func<EMP, bool> del)
{
var objK = objEmp.Where(del).ToList();
List<EMP> lst= new List<EMP>();

return lst;
}

}


hope This will help you.

Ajay
ajaypatelfromsanthal.blogspot.in

Ajayk3, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response