mitali
mitali

Reputation: 153

Get name from table 1 whose id in table 2 using linq

I want to get the details of some attribute(session,trade,student etc.), and i have their id's in ProgressCard table, using linq. and save the resulting data in XMl

I am doing this

    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>();
        }
    }

but gives me unexpected values..

<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>

please help me to track the mistakes.

Upvotes: 0

Views: 284

Answers (1)

TGlatzer
TGlatzer

Reputation: 6248

                            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(),

This block will return nothing but IEnumerables of your values. Add Single() or First() before ToString():

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

Upvotes: 1

Related Questions