Stack Exchange
increase Performance of the code
How can rid of foreach and still get the same output. How can i increase the performance of the code by getting rid of the foreach loop.
public List<tblcourse> GetData(string value)
{
    testEntities1 db = new testEntities1();

    int v = Convert.ToInt32(value);
    testEntities1 t = new testEntities1();
    var u = (from g in t.tblcourses
             select new  { g.C_Id,  g.C_Name }).ToList();

    List<tblcourse> lisstt = new List<tblcourse>();

    foreach (var item in u)
    {
        tblcourse b = new tblcourse();
        b.C_Id = item.C_Id;
        b.C_Name = item.C_Name;
        lisstt.Add(b);
    }

    return lisstt;
}


THe two questions you pose are orthogonal, it is easy enough to not have the foreach loop but removing this will probably not improve performance.

You can increase performance by removing the lines of code that aren't used.  E.g. intialising 'db' and 'v'.

What is use of variable v? You might missing where condition on tblcourses..?

Try this:-
var u = (from g in t.tblcourses
             select new tblcourse { C_Id = g.C_Id, C_Name = g.C_Name }).ToList();

Instead of selecting anonymous type, you can directly fill your custom Type.

Let LINQ create the tblcourse objects instead of anonymous objects.
public List<tblcourse> GetData(string value)
{
    return (from g in db.tblcourses
            select new tblcourse() { C_Id = g.C_Id,  C_Name = g.C_Name }).ToList();
}


I'd reduce it to something like:
Note: I've removed the unused variables. I've also assumed that testEntities1 is an Entity Framework DbContext and requires disposing. I've also used the results variable to temporarily hold a reference to the list so it can be easily debugged by adding a breakpoint.
public IList<tblcourse> GetData()
{
    using (var testContext = new testEntities1())
    {
        var results =
            testContext.tblcourses
                .Select(c => new tblcourse() { C_Id = c.C_Id, C_Name = c.C_Name })
                .ToList();

        return results;
    }
}

Of course replacing the foreach with a LINQ statement probably wont improve performance much, but it's probably more maintainable now.
You really want to look at a guide for best practices of naming class members in C#.

If you mean how do you get it to look cleaner (as the performance gain is negligable), you can do so like this:
public List<tblcourse> GetData(string value)
{
    return (from g in new testEntities1().tblcourses
             select new tblcourse { C_Id = g.C_Id, C_Name = g.C_Name }).ToList();
}

