2012-10-04 83 views
0

我想獲得一些屬性(會話,交易,學生等)的詳細信息,並且我使用linq在ProgressCard表中獲得了他們的id。 並保存生成的數據以XML從表1中獲取名稱,其表2中的id使用linq

我這樣做

public List<ShowProgressCard> GetCardListToShow() 
    { 
     try 
     { 
      List<ShowProgressCard> CardList = new List<ShowProgressCard>(); 
      using (ProgressCardLINQDataContext c = new ProgressCardLINQDataContext()) 
      { 
       CardList = (from card in c.GetTable<T_PROGRESSCARD>() 
          where card.RecordStatus.Equals(RecordStatus.Active) 
          select new ShowProgressCard 
          { 
           Session=(from session in c.T_SESSIONs 
             where session.Id.Equals(card.SessionId) 
             select session.Name).ToString(), 
           Trade=(from trade in c.T_TRADEs 
             where trade.Id.Equals(card.TradeId) 
             select trade.Name).ToString(), 
           Student=(from student in c.T_STUDENTs 
             where student.Id.Equals(card.StudentId) 
             select student.Name).ToString(), 
           Test=(from test in c.T_TESTs 
             where test.Id.Equals(card.TestId) 
             select test.Name).ToString(), 
           MaxMarks = (from test in c.T_TESTs 
              where test.Id.Equals(card.TestId) 
              select test.MaxMarks).ToString(), 
           MarksObtain=card.MarksObtain.ToString(), 
           Percentage=card.Percentage.ToString("N2") 
          }).ToList<ShowProgressCard>(); 
      } 
      return CardList; 
     } 
     catch 
     { 
      return new List<ShowProgressCard>(); 
     } 
    } 

但讓我意外的值..

<ShowProgressCard> 
<Session>System.Collections.Generic.List`1[System.String]</Session> 
<Trade>System.Collections.Generic.List`1[System.String]</Trade> 
<Student>System.Collections.Generic.List`1[System.String]</Student> 
<Test>System.Collections.Generic.List`1[System.String]</Test> 
<MaxMarks>System.Collections.Generic.List`1[System.Int32]</MaxMarks> 
<MarksObtain>123</MarksObtain> 
<Percentage>0.000000000000000e+000</Percentage> 
</ShowProgressCard> 

請幫我跟蹤的錯誤。

回答

1
      Session=(from session in c.T_SESSIONs 
            where session.Id.Equals(card.SessionId) 
            select session.Name).ToString(), 
          Trade=(from trade in c.T_TRADEs 
            where trade.Id.Equals(card.TradeId) 
            select trade.Name).ToString(), 
          Student=(from student in c.T_STUDENTs 
            where student.Id.Equals(card.StudentId) 
            select student.Name).ToString(), 
          Test=(from test in c.T_TESTs 
            where test.Id.Equals(card.TestId) 
            select test.Name).ToString(), 
          MaxMarks = (from test in c.T_TESTs 
             where test.Id.Equals(card.TestId) 
             select test.MaxMarks).ToString(), 

該塊只會返回您的值的IEnumerables。在ToString()之前添加Single()或First():

MaxMarks = (from test in c.T_TESTs where test.Id.Equals(card.TestId) select test.MaxMarks).Single().ToString(), 
+0

非常感謝。 – mitali

+0

你能否請我青睞一件事,那就是我想要拿到的百分比是2位小數。我嘗試ToString方法中的「N2」,但它的工作量很大。你知道爲什麼嗎? – mitali

+0

MSDN知道「C#字符串格式」,但我不知道從內存中。 – TGlatzer

相關問題