none
DataContext - Log Funktion RRS feed

  • Frage

  • Hallo,
    kurze Frage.
    In
    strwLog
    steht erst was drin, wenn das
    frmR1.DataSource = qry1;
    ausgeführt wurde.

    Kann jemand eine Erklärung abgeben?

    Es muss ja dann eine Referenz sein, es gibt sonst keine weitere Zuweisung.

    Grüße Oliver

    var qry2 = _Db.CountryRegions
                .Join(_Db.StateProvinces,
                cr => cr.CountryRegionCode,
                sp => sp.CountryRegionCode,
                (cr, sp) => new
                {
                   cr.CountryRegionCode,
                   CountryRegionName = cr.Name,
                   sp.StateProvinceCode,
                   StateProvinceName = sp.Name
                })
                .OrderBy(cr => cr.CountryRegionName)
                .ThenBy(sp => sp.StateProvinceName);
    
             StringBuilder strb = new StringBuilder();
             StringWriter strwLog = new StringWriter(strb);
    
             _Db.Log = strwLog;
    
             FrmResultGrid frmR1 = new FrmResultGrid();
             frmR1.DataSource = qry1;
             frmR1.Show();
    
    		 
    		 
    public partial class FrmResultGrid : Form
       {
          public object DataSource
          {
             set
             {            
                dgvResult.Rows.Clear();
                dgvResult.DataSource = value;
             }
          }
    
          public string FormTitle
          {
             set
             {
                this.Text = "Result: " + value;
             }
          }
          
          public FrmResultGrid()
          {
             InitializeComponent();
          }
       }
       
       .....
        // Zusammenfassung:
        //     Represents the main entry point for the LINQ to SQL framework.
        public class DataContext : IDisposable
        {
            public DataContext(string fileOrServerOrConnection);
            //
            // Zusammenfassung:
            //     Gets or sets the System.Data.Linq.DataLoadOptions associated with this System.Data.Linq.DataContext.
            //
            // Rückgabewerte:
            //     The prefetch load options for related data.
            public DataLoadOptions LoadOptions { get; set; }
            //
            // Zusammenfassung:
            //     Specifies the destination to write the SQL query or command.
            //
            // Rückgabewerte:
            //     The System.IO.TextReader to use for writing the command.
            public TextWriter Log { get; set; }

    Mittwoch, 18. April 2012 15:49

Antworten

  • Hallo Oliver,

    da steht zum einen erst was drin, weil ein SQL Befehl erst zum Zeitpunkt der Ausführung generiert wird,
    siehe u. a.: Query Execution

    Zum anderen wohl weil Linq erst die Zunge entknoten muss bei strwLog => pseudo-ungarische Notation sollte man vermeiden.
    Ebenso kann ein Join leichter lesbar werden, wenn man schreibt:

    var countyAndProvinceQuery = 
    	from cr in _Db.CountryRegions
    	join sp in _Db.StateProvinces
    		on cr.CountryRegionCode equals sp.CountryRegionCode
    	orderby cr.CountryRegionName, sp.StateProvinceName
    	select new {
                   cr.CountryRegionCode,
                   CountryRegionName = cr.Name,
                   sp.StateProvinceCode,
                   StateProvinceName = sp.Name
                });
    
    Gruß Elmar
    • Als Antwort markiert O. Stippe Mittwoch, 18. April 2012 19:11
    Mittwoch, 18. April 2012 17:09
    Beantworter