Copy items from one List to another List without foreach using Entity Framework in Linq Query using C# in ASP.Net

AnushaM
 
on Jan 31, 2018 05:21 AM
Sample_181267.zip
40376 Views

copy the list records to another list without using loop.. I have created a  temporaray list to store items , then on final click those items has to be moved to my DB table using loop. now without using the loop i should store values  to a temporary list and on final click on save button, the records has to be stored to DB table

namespace X360BankGenie
{
    public partial class View_page : System.Web.UI.Page
    {
        //creating database object
 
        x360Entities db = new x360Entities();
       
        //web method to fetch data from db table to the new page
        [WebMethod]
        public static string GetStudent_Details()
        {
            //create db object
 
            x360Entities db = new x360Entities();
 
            //fetching records frm db and storing in Student_Details
 
            var Student_Details = db.students.ToList();// fetching records frm db
            if (Student_Details.Count > 0)
            {
                List<CommonLayer.SessionObjects.Student_Details> objstudent_DetailsLst = new List<CommonLayer.SessionObjects.Student_Details>();//creating list of student details class to bind the value frm db.
                foreach (var item in Student_Details)
                {
                   SessionObjects.Student_Details  objstudent_Details = new SessionObjects.Student_Details();//creatimg a class object inside the commom layer session objects
 
                    // binding data to object
 
                    objstudent_Details.slno = item.slno;
                    objstudent_Details.college = item.college;
                    objstudent_Details.name = item.name;
                    objstudent_Details.usn = item.usn.ToString();
                    objstudent_DetailsLst.Add(objstudent_Details);
                }
                //creating list and list object
                 
                
 
 
 
 
                //for serializing data
 
                JavaScriptSerializer objJavaScriptSerializer = new JavaScriptSerializer();
                return objJavaScriptSerializer.Serialize(Student_Details);
            }
            else
            {
                return null;
            }
        }
    }
}
Download FREE API for Word, Excel and PDF in ASP.Net: Download
kalpesh
 
on Feb 02, 2018 12:01 AM

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