使用linq从表1中获取其表2中id的名称

时间:2012-10-04 10:06:04

标签: c# asp.net xml linq-to-sql

我想获得一些属性(会话,交易,学生等)的详细信息,并且我在ProgressCard表中使用linq获取了他们的id。 并将结果数据保存在XMl

我这样做

    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 个答案:

答案 0 :(得分: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(),