Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6153005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:01:14+00:00 2026-05-23T20:01:14+00:00

I have these below LINQ to SQL queries var kayitlarFiltreli = from rows in

  • 0

I have these below LINQ to SQL queries

var kayitlarFiltreli = from rows in db.TBLP1CARIs
                       orderby rows.ID descending
                       where rows.HESAPADI.ToLower().Contains(filter.ToLower()) ||
                                              (rows.CARITURU == "Bireysel" ?
                                               rows.B_ADSOYAD.ToLower().Contains(filter.ToLower()) :
                                               rows.K_FIRMAADI.ToLower().Contains(filter.ToLower())) ||
                                               rows.ID.ToString().Contains(filter)
                       select rows;

var kayitlarBakiyeli = from rows in kayitlarFiltreli
                       select new
                                   {
                                       HESAPNO = rows.ID,
                                       HESAPADI = rows.HESAPADI,
                                       CARIADI = (rows.CARITURU == "Bireysel" ? rows.B_ADSOYAD : rows.K_FIRMAADI),
                                       Bakiye = get_bakiye(rows.ID, rows.LISTEPARABIRIMI)
                                   };

var kayitlarSon = from rows in kayitlarBakiyeli
                  select new
                            {  rows.HESAPNO,
                               rows.HESAPADI,
                               rows.CARIADI,
                               Bakiye = rows.Bakiye.Contains(".") == true ?
                                        rows.Bakiye.TrimEnd('0').TrimEnd('.') :
                                        rows.Bakiye

                            };

I am having performance problem I mean the queries response at least after 15secs, and when it is deployed to the website it takes at least 5 secs for the page which is using these queries to fill a GridView.get_bakiye(p1,p2,..) is a long method with a for, a foreach and a Linq-to-SQL query in it.I think the most of time is spent on get_bakiye I struggled with it already and reduced the response time like 2 secs, however it is still slow.And I am trying to get the above queries work faster.

I tried

var kayitlarSirali =  from rows in db.TBLP1CARIs
                      orderby rows.ID descending
                      select rows;

var kayitlarFiltreli = from rows in kayitlarSirali
                       where rows.HESAPADI.ToLower().Contains(filter.ToLower()) ||
                                        (rows.CARITURU == "Bireysel" ? 
                                         rows.B_ADSOYAD.ToLower().Contains(filter.ToLower()) : 
                                         rows.K_FIRMAADI.ToLower().Contains(filter.ToLower())) ||
                                         rows.ID.ToString().Contains(filter)
                       select rows;

And the rest is the same.
Basically I just seperated the filtering part with Contains(), which I am not sure if that helps so much.

Is it good to seperate where‘s I mean filters when querying the database, and is it better for performance to query the database once and get the results into an in-memory IQueryable and do the rest on it?

What do you recommend for these queries to work faster?
This is the get_bakiye() method which is not something I wrote fully but I am supposed to make it perform faster.

public static string get_bakiye(int cari_id, string birim_kod)
        {
            return get_bakiye(cari_id, DAL.DAOCari.GetEntity(cari_id).LISTEPARABIRIMI, null,false);
        }

public static string get_bakiye(int cari_id, string birim_kod, List<BAL.P_CariBakiyeTablosu> custom_rapor, bool borcluTespit)
        {
            VeriyazDBDataContext db = new VeriyazDBDataContext(); db.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

            decimal return_bakiye = 0;

            if (birim_kod == null || birim_kod.Trim() == "") //default
                birim_kod = "TL";

            //devir bakiyesini hesapla:
            List<BAL.P_CariBakiyeTablosu> bakiyeler = new List<BAL.P_CariBakiyeTablosu>();
            if (custom_rapor == null)
                bakiyeler = CariBakiyeRaporuOlustur(cari_id, true);
            else
                bakiyeler = custom_rapor;

            bakiyeler.RemoveAt(0);

            List<TBLP1DOVIZTANIMLARI> dovizTanimlariTumListe = DAL.DAOdoviztanimlari.SelectAll().ToList();
            //devirleri hesaplarken döviztanimlari tablosundaki varsayılan kuru kullanıyor
            for (int i = 0; i < bakiyeler.Count; i++)
            {
                if (bakiyeler[i].DOVIZ == birim_kod)
                {
                    return_bakiye = return_bakiye + Convert.ToDecimal(bakiyeler[i].DEVIR);
                }
                else
                {
                    decimal from_kur = 1;
                    from_kur = from_kur = dovizTanimlariTumListe.Where(rows => rows.TBLP1DOVIZLER.KOD == bakiyeler[i].DOVIZ).FirstOrDefault().VARSAYILANKUR.GetValueOrDefault(1);

                    decimal to_kur = 1;
                    to_kur = dovizTanimlariTumListe.Where(rows => rows.TBLP1DOVIZLER.KOD == birim_kod).First().VARSAYILANKUR.GetValueOrDefault(1);


                    return_bakiye = return_bakiye + (Convert.ToDecimal(bakiyeler[i].DEVIR) * (from_kur / to_kur));
                }
            }

            //islem bakiyesini hesapla:

            var islemler = from rows in db.TBLP1ISLEMs
                           where
                           rows.CARI_ID == cari_id &&
                           rows.TEKLIF.GetValueOrDefault(false) == false &&
                           rows.SOZLESME.GetValueOrDefault(false) == false &&
                           (rows.SIPARISDURUMU == "İşlem Tamamlandı" || 
                            rows.SIPARISDURUMU == "Hazırlanıyor" || 
                            rows.SIPARISDURUMU == "" || 
                            rows.SIPARISDURUMU == null)
                           select rows;

            //var dovizKuruOlanIslemler = from dovizKuruRow in db.TBLP1DOVIZKURUs
            //                            select dovizKuruRow.ISLEM_ID;

            foreach (var item in islemler)
            {
                decimal from_kur = 1;
                decimal fromKurVarsayilan = 1;
                //belirtilen dövizin varsayılanını çekiyor
                fromKurVarsayilan = dovizTanimlariTumListe.Where(rows => rows.TBLP1DOVIZLER.KOD == item.PARABIRIMI).FirstOrDefault().VARSAYILANKUR.GetValueOrDefault(1);

                try
                {
                    from_kur = item.KURDEGERI.Value;
                    //aşağıdaki satırda dövizkuru tablosundan işleme ait kuru çekerek hesap yapıyordu, işlem tablosuna KURDEGERİ kolonu ekleyince
                    //buna gerek kalmadı, yukarıdaki satırda işleme ait kur değeri işlem tablosundan çekiyor.
                    //from_kur = item.TBLP1DOVIZKURUs.Where(rows => rows.DOVIZBIRIM == item.PARABIRIMI).FirstOrDefault().KUR.GetValueOrDefault();
                }
                catch
                {
                    from_kur = fromKurVarsayilan;
                }

                //carinin para biriminin varsayılan kurunu çekiyor
                decimal to_kur = 1;
                decimal toKurVarsayilan = 1;

                toKurVarsayilan = dovizTanimlariTumListe.Where(rows => rows.TBLP1DOVIZLER.KOD == birim_kod).FirstOrDefault().VARSAYILANKUR.GetValueOrDefault(1);

                to_kur = toKurVarsayilan;


                if (item.CARIISLEMTURU == "BORC")
                {
                    return_bakiye = return_bakiye + (Convert.ToDecimal(item.GENELTOPLAM) * (from_kur / to_kur));
                }
                if (item.CARIISLEMTURU == "ALACAK")
                {
                    return_bakiye = return_bakiye - (Convert.ToDecimal(item.GENELTOPLAM) * (from_kur / to_kur));
                }
            }

            string returnBakiyeParaFormatli = DAL.Format.ParaDuzenle.ParaFormatDuzenle(return_bakiye.ToString());
            if (borcluTespit==true)
            {
                return return_bakiye.ToString();
            }
            if (returnBakiyeParaFormatli.Contains(".") == true)
            {
                return returnBakiyeParaFormatli.TrimEnd('0').TrimEnd('.') + " " + birim_kod;

            }
            else
            {
                return returnBakiyeParaFormatli + " " + birim_kod;
            }

        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:01:15+00:00Added an answer on May 23, 2026 at 8:01 pm

    In general i think you need to understand what causes Linq to Sql to execute a query against your database. In general, extension methods such as ToList(), First(), FirstOrDefault(), Single() will cause Linq To Sql to execute a command against the database. One line that does concern me is:

    List<TBLP1DOVIZTANIMLARI> dovizTanimlariTumListe = DAL.DAOdoviztanimlari.SelectAll().ToList();
    

    This seems to be getting every row from the database table that DAOdoviztanimlari is mapped to. The result of this is then queried in memory.

    This then happens for every record in the queries that call get_bakiye()!

    Ultimately (perfect world) you want get_bakiye() to not contain any of the extension methods i have mentioned and to return IQueryable<string> then let Linq to SQL descide how it optimizes and executes the SQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a linq query like the one below var LinqResult =
I have the below LINQ to SQL method that takes an inordinate amount of
rbnResubmission.Items.FindByValue(Yes).Attributes.Add(onclick, getCheckedRadioFieldResubmission(this)); rbnResubmission.Items.FindByValue(No).Attributes.Add(onclick, getCheckedRadioFieldResubmission(this)); So I have these click events for showing rows in
I have created some extra functionality on my Linq-to-SQL classes to make things easier
When using compiled queries in entity framework (or linq-to-sql) in combination with SQL Server,
I have the below SQL which works just fine: SELECT Message, CreateDate, AccountId, AlertTypeId
I have the below LINQ that is returning zero IF there aren't any Addresses(Inner
I have an issue with a linq to sql query - performance wise. What
i'm new to asp.net mvc. I'm displaying data from a linq to sql query
I use Linq to Entities to retrieve my records from DB. The function below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.