1
          List<VideoInfo> vobj = new List<VideoInfo>();
         vobj = (from vid in db.VideoInfoes.Where(c => c.IsActive == true)
                orderby vid.VideoId  descending
                select vid
                ).ToList();
        return View(vobj);

This is the orignal Query to bring the list of all the video info. There is another table called profile which has the Profile Picture which I need along with the video Info. So after going through an article on EF I came up with some thing like this..

  vobj = (from vid in db.VideoInfoes.Where(c => c.IsActive == true)
                 select new
                {
                 ProfileId = vid.ProfileId,
                 ProfilePictureUrl = vid.ProfileInfo.ProfilePictureUrl
                }
                orderby vid.VideoId  descending
                 select vid
                ).ToList();
        return View(vobj);

ProfileId is the foreign key. But this is not even compiling..Its showing red syntax error after the closing curly brackets and orderby.

2 Answers 2

2

You should do OrderBy before Select since the property you want to order is not contained in your new select:

var vobj = (from vid in db.VideoInfoes.Where(c => c.IsActive == true)
        orderby vid.VideoId  descending
        select new
            {
             ProfileId = vid.ProfileId,
             ProfilePictureUrl = vid.ProfileInfo.ProfilePictureUrl
            }
        ).ToList();

Aslo I feel more comfortable if using lambda syntax:

var vobj = db.VideoInfoes.Where(c => c.IsActive)
          .OrderByDescending(c => c.VideoId)
          .Select(c => new {
                ProfileId = vid.ProfileId,
                ProfilePictureUrl = vid.ProfileInfo.ProfilePictureUrl
            }).ToList();
Sign up to request clarification or add additional context in comments.

6 Comments

yes I tried this.. but vobj is of type VideoInfo so its not allowing me to do ToList..It says cannot convert anonymous type to Generic type VideoInfo
@Scorpio: edited, you don't need to declare ` List<VideoInfo> vobj = new List<VideoInfo>();` since vobj is now anonymous type, use var instead
@Scorpio select new will project the sequence into an enumerable of anonymous type. If you want an enumerable of VideoInfo you should just select vid as you had at the end. (you don't even need a select clause if you use the method chain syntax in that case).
@CuongLe I dont get this... My view is expecting type @ model IEnumerable<Data.VideoInfo> ...how can I use var ?? its bit confusing at this poin for me
@Scorpio: You need to take a look anonymous type in C#
|
1

Your function can be corrected and made much simpler as follows:

return View(
   db
   .VideoInfoes
   .Where(videoInfo => videoInfo.IsActive)
   .OrderByDescending(videoInfo => videoInfo.VideoID)
   .ToList());

You will be able to access the ProfileInfo of each VideoInfo in the list, so long as the db connection is not disposed. Or you can access other ProfileInfo records using the ProfileId property in another query with another context later. It depends what you would like to do with the data; (I'm not sure what the View function is doing, for example.)

For example:

// where 'data' was what was returned from the above query
// and assuming the above 'db' connection is still open
foreach (VideoInfo videoInfo in data) 
{
   ProfileInfo profileInfo = videoInfo.ProfileInfo;

   // do something with profileInfo
}

// or if the connection was disposed:
using (MyEFModel db = new MyEFModel())
   foreach (VideoInfo videoInfo in data) 
   {
      int profileID = videoInfo.ProfileId;
      ProfileInfo profileInfo =
         db
         .ProfileInfoes
         .Single(row => row.ID == profileID);

      // do something with profileInfo
   }

1 Comment

thts true the primary - foreign key is already mapped in DB ..I dont know what was I thinking.. The previous query work's correctly ...its just that I have to go one level deep i.e. VideoInfoModel.ProfileInfo.ProfilePictureUrl...anyways thank for all the help..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.