Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

How Do I Select Fields from a Nested ICollection?

$
0
0

First, I am new to LINQ and Entities Framework, so I am still learning the syntax and how the relationships work. Surprisingly LINQ is nothing like SQL as people claim. Anyway I need to know how to Select fields from a nested ICollection within a Select statement. The following query gives me all the data I need, but too much data. I need to be able to pull just the parts of the Comments, Images, and Ratings ICollections that I want in the UI because otherwise the api controller displays everything. 

 public async Task<Posts[]> GetPosts()
        {
            var posts = await _context.Posts.Where(post => post.Active == true && post.Adminban == false && post.Site == GlobalStatic.SITENUMBER())
                            .Select(post => new Posts { 
                            Postid = post.Postid,
                            Title = post.Title,
                            Description = post.Description,
                            Dateposted = post.Dateposted,
                            Video = post.Video,
                            Videostream = post.Videostream,
                            Location = post.Location,
                            Tags = post.Tags,
                            Cap = post.Cap,
                            Titletag = post.Titletag,
                            Metatag = post.Metatag,
                            Link = post.Link,
                            Linkurl = post.Linkurl,
                            Comments = post.Comments,
                            Ratings = post.Ratings,
                            Images = post.Images,
                            WorldareaNavigation = new Worldarea
                            {
                                Worldarea1 = post.WorldareaNavigation.Worldarea1
                            },
                            RegionNavigation = new Regions
                            {
                                Regionname = post.RegionNavigation.Regionname
                            },
                            CityNavigation = new Cities
                            {
                                City = post.CityNavigation.City
                            },
                            CategoryNavigation = new Categories
                            {
                                Categoryname = post.CategoryNavigation.Categoryname
                            },
                            SubcategoryNavigation = new Subcategories
                            {
                                Subcategoryname = post.SubcategoryNavigation.Subcategoryname
                            },
                            Subcategory2Navigation = new Subcategory2
                            {
                                Subcategory2name = post.Subcategory2Navigation.Subcategory2name
                            }
                            })
                        .ToArrayAsync();
            return posts;
        }

The problem is that because Comments, Ratings, and Images are not Navigation properties I cannot select values from them like I can with categories and locations. I have tried adding nested select statements to Images but I have the wrong syntax. Stuff like:

Images = post.Images.Select(img => img.Imageid == Imageid),


Images = post.Images.Select(img => new Images { Imageid = img.Imageid, Image = img.Image }),

Neither of the above seem to work. I'm sure the answer is something simple but I don't know the syntax well enough to write it right.

The Posts method has the following entries for Images, Comments, and Ratings

public virtual ICollection<Comments> Comments { get; set; }
        public virtual ICollection<Images> Images { get; set; }
        public virtual ICollection<Ratings> Ratings { get; set; }

public Posts()
        {
            Comments = new HashSet<Comments>();
            Images = new HashSet<Images>();
            Ratings = new HashSet<Ratings>();
        }




Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>