Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm new, and I need beginner's help.
I have built a system that searches for words in a database, and if it does, it displays within a list.
I realized that the way I do it is very slow and cumbersome, I want to know how to do it more correctly.

Regards

What I have tried:

LblSrc.Text = src;

               List<Piska> allPiskaot = Blm.BLSrcCustomers(src);

bl-
public static List<Piska> BLSrcCustomers(string src)
       {
           List<Piska> EntityList = new List<Piska>();
           List<Piskaot> DBList = DAlManager.GetSrcPiska(src);
           foreach (var item in DBList)
           {
               EntityList.Add(ConvertPiskaDbToPiskaEntity(item));
           }
           return EntityList;
       }



       public static Piska ConvertPiskaDbToPiskaEntity(Piskaot piska)
       {
           return new Piska
           {
               Id = piska.Id,
               Tags = piska.Tags,
               PTitle = piska.Ptitle,
               Tohen = piska.Tohen,
               Edited = piska.edited,
               Book = piska.book,
               Chapter = piska.chapter
           };
       }


dal-
public static List<Piskaot> GetSrcPiska(string src)
{
    using (var context = new DBNafshenuEntities())
    {
        var result = context.Piskaot.ToList();
        List<Piskaot> returnList = new List<Piskaot>();
        foreach (var item in result)
        {

            if (item.Tohen.Contains(src))
            {

                returnList.Add(item);
            }

        }
        return returnList;
    }
}
Posted
Updated 6-Jul-20 3:45am
Comments
PIEBALDconsult 5-Jul-20 1:20am    
I see a number of issues.
The use of .ToList() is a red-flag for me, but more importantly is that you appear to be getting the full list of data items, converting them all to objects, and _then_ filtering then -- ideally the filtering should be done within the database.
Member 14683862 5-Jul-20 3:39am    
Could you please tell me how to write code that can work directly on the database?

“More correctly” is a relative term. you should expect to be able to write better code until you’ve written a lot more beginner code. If your code meets the stated requirements, and runs without errors, it’s fine.
 
Share this answer
 
Comments
Member 14683862 5-Jul-20 3:38am    
Right now it's working fine, I'm scared about the time loading as the data meter grows
#realJSOP 5-Jul-20 10:11am    
Well, it looks like you're using EntityFramework, so I can't comment because I refuse to use it. I thought it deserialized the data into entities for you, but I might be wrong...
Member 14683862 5-Jul-20 11:13am    
If you use SQL, I want to know how to add SQL code in VISUALSTUDIO-

String connectionString = WebConfigurationManager.ConnectionStrings["DBNafshenuConnectionString"].ToString();
StringBuilder sb = new StringBuilder();


using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand commandPodesavanje = new SqlCommand("SELECT * FROM Users", connection))
{
SqlDataReader reader = commandPodesavanje.ExecuteReader();
while (reader.Read())
{

}
reader.Close();
}
}
Assuming you're using Entity Framework, you can pass the search into the database so that you're not loading more records than necessary.

DAL:
C#
public static List<Piskaot> GetSrcPiska(string src)
{
    using (var context = new DBNafshenuEntities())
    {
        return context.Piskaot.Where(item => item.Tohen.Contains(src)).ToList();
    }
}
 
Share this answer
 

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