2012-11-16 72 views
1

我有以下代碼。 所有20個對象似乎都創建好了。
第一個foreach然後工作正常,遍歷全部20.
使用linq的第二個例子很好地工作。使用IEnumerable時針對單個報告

然後,是否可以僅使用諸如ReportKey之類的屬性來定位其中一個對象,然後針對該對象運行方法RunThisReport?或者,因爲我使用了IEnumerable<>這種類型,我是否已經陷入了死衚衕?

static void Main(string[] args) { 

    var models = SelectReports("SELECT * FROM 20RecordTable"); 

    //1.running the method for each 
    foreach(myReport x in models) { 
     Console.WriteLine("Doubled key:{0}", x.myReportKeyDoubled().ToString()); 
    } 

    //2.linq sample 
    var result = from sample in models 
        select sample.ReportName; 
    foreach(string x in result) { 
     Console.WriteLine(Convert.ToString(x)); 
    } 

    //3. target a single report say reportKey 512 and run the method RunThisReport? 

    Console.WriteLine("Press [enter] to exit"); 
    Console.Read(); 
} 

static IEnumerable<myReport> SelectReports(string myCommandText) { 

    var connectionString = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString; 
    using(var conn = new SqlConnection(connectionString)) 
    using(var cmd = conn.CreateCommand()) { 
     conn.Open(); 
     cmd.CommandText = myCommandText; 
     using(var reader = cmd.ExecuteReader()) { 

      while(reader.Read()) { 
       yield return new myReport { 

        ReportKey = reader.GetInt32(reader.GetOrdinal("ReportKey")), 
        ReportName = reader.GetString(reader.GetOrdinal("ReportName")), 
        ReportDescription = reader.GetString(reader.GetOrdinal("ReportDescription")), 
        ReportTechDescription = reader.GetString(reader.GetOrdinal("ReportTechDescription ")) 

       }; 
      } 
     } 
    } 
} 

public class myReport { 

    public int ReportKey { get; set; } 
    public string ReportName { get; set; } 
    public string ReportDescription { get; set; } 
    public string ReportTechDescription { get; set; } 

    public int myReportKeyDoubled() { 
     return ReportKey*2; 
    } 
    public string RunThisReport(){ 
     return this.ReportName + " needs to be run via" + this.ReportTechDescription; 
    }   
} 
+1

你避免像'(字符串)閱讀器[「ReportName」]''代碼的任何原因? –

+0

models.First(i => i.ReportKey ==「YOUR-KEY-VALUE」)。RunThisReport(); – kingdango

+0

@ ta.speot.is替換'reader.GetString(reader.GetOrdinal(「ReportName」))'? – whytheq

回答

2
var report = models.SingleOrDefault(m => m.ReportKey == 512); 

if (report != null) 
    report.RunThisReport(); 

或用理解語法(醜陋的,是嗎?):

var report = (from m in models 
       where m.ReportKey == 512 
       select m).SingleOrDefault(); 

BTW與Dapper您的代碼如下:

static IEnumerable<myReport> SelectReports(string myCommandText) 
{  
    var connectionString = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString; 
    using(var conn = new SqlConnection(connectionString)) 
    { 
     conn.Open(); 
     return conn.Query<myReport>(myCommandText);  
    } 
} 

小巧玲瓏可通過的NuGet。並與ADO.NET

+0

SingleOrDefault接受一個謂詞。 –

+0

@ ta.speot.is謝謝,總是忘記! –

+0

我經常這樣做,但R#爲我打電話。 –

1
models.First(x => x.ReportKey == 42).RunThisReport(); 
+1

「ReportKey == 128375443」呢? –

+0

@lazyberezovsky? – whytheq

+0

@whytheq如果沒有找到這樣的鍵的報告,這個方法會拋出'NullReferenceException' –

1

如果我讀這正確的你與獲得使用LINQ的對象努力工作時,是我的選擇。這裏最簡單的事情就是使用LINQ擴展方法。我相信這是你在找什麼:

models.First(i => i.ReportKey == yourReportKey).RunThisReport(); 

還是要澄清一下是怎麼回事:

// Get the report that matches the key 
myReport report = models.First(i => i.ReportKey == "YOUR-KEY-VALUE"); 

// Call RunThisReport for that report. 
report.RunThisReport(); 

這也適用於集合:

models.Where(i => i.ReportKey == yourReportKey).ForEach(report => report.RunThisReport()); 

希望幫助!如果不是,請澄清您的問題,我很樂意提供幫助。

+0

+1;謝謝擴展答案 – whytheq

相關問題