Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I'm trying to select the 30 latest entries in my database, but I want to select them from 4 different tables.

Is there a way to do this clean and quick using linq?

At the end of the query I want to store these objects in a list.

Thanks in advance!
Posted
Updated 5-May-13 20:57pm
v2
Comments
CHill60 25-Apr-13 11:08am    
If you are accessing your database why not use an OleDbDataReader or SqlDataReader as appropriate and write the sql command to join across the tables?

Something like this?

var items =
    (from a in _tableOne
    join b in _tableTwo on a.ID equals b.A_ID
    join c in _tableThree on a.ID equals c.A_ID
    join d in _tableFour on a.ID equals d.A_ID
    orderby a.ID descending
    select new { a.Name, b.B_Field, c.C_Field d.D_Field}).Take(30)
 
Share this answer
 
Comments
Sam Van den Bossche 25-Apr-13 16:33pm    
yes that's a start.. now I have to order them on a date that is stored in each table separately.
I tried orderby b.date, c.date, d.date
but that's not the way to go
Ordering dates like that works for me.

What format are your dates in? You might get different results depending on that.

What order are your results in?
 
Share this answer
 
C#
var feedList =
                    ((from video in context.Video
                      orderby video.Date descending
                      select new { TypeID = video.VideoID, CodeID = video.CodeID, Date = video.Date }).Take(50))
                     .Union((from comment in context.Comment
                             orderby comment.Date descending
                             select new { TypeID = comment.CommentID, CodeID = comment.CodeID, Date = comment.Date }).Take(50))
                        .Union((from share in context.Share
                                orderby share.DateTime descending
                                select new { TypeID = share.ShareID, CodeID = (int?)share.CodeID, Date = share.DateTime }).Take(50))
                        .Union((from events in context.Event
                                orderby events.CreateDate
                                select new { TypeID = events.EventID, CodeID = events.CodeID, Date = events.CreateDate }).Take(50))
                               .ToList();



                foreach (var f in feedList)
                {
                    var feed = new FeedListItemsContract();
                    {
                        feed.TypeId = f.TypeID;
                        var blCode = new BLCode();
                        feed.CodeName = blCode.GetCodeNameById((int)f.CodeID);
                        feed.Date = f.Date;
                    }
                    result.Add(feed);
                }


                var resultSorted = result.OrderByDescending(s => s.Date).ToList();
                return resultSorted;


that's what I needed to do to make it work..
important is that every select returns the same ammount of tables!

thanks for the help!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900