Hi AnushaM,
Try by using a combination of Lambdas and LINQ to achieve the solution. The Select function is a projection style method which will apply the passed in delegate (or lambda in this case) to every value in the original collection. The result will be returned in a new IEnumerable<TargetType>. The .ToList call is an extension method which will convert this IEnumerable<TargetType> into a List<TargetType>.
Refer the below Sample code and implement the logic as per your code logic here i used the Customer table from Northwind database to use it in Entity Framwork.
C#
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetCustomers();
}
public void GetCustomers()
{
NorthwindEntities entities = new NorthwindEntities();
List<Customer> customerList = new List<Customer>();
customerList = (from customer in entities.Customers
select customer).Take(4)
.Select(x => new Customer()
{CustomerID = x.CustomerID
,City = x.City
,ContactName = x.ContactName
,Country = x.Country
}).ToList();
}
}
// Your class
public class Customer
{
public string CustomerID;
public string ContactName;
public string City;
public string Country;
}
Screenshot
