0

This is my code enter image description here

 public IEnumerable<InsightPost> InsightsPosts { get; set; }
 public void OnGet()
 {
      InsightsPosts = _db.InsightPosts.Where(p => p.Tags.Contains("test")).SelectMany(s => new { s.Title, s.Description });
 }

My database has more columns but I only need a select few. What am I doing wrong here.

enter image description here

Zippy
  • 455
  • 1
  • 11
  • 25
  • Have you tried using Select instead of SelectMany? – TGnat Apr 01 '22 at 13:24
  • yep it says to use selectmany – Zippy Apr 01 '22 at 13:43
  • @Zippy you dont need use selectmany, can use .Select(s => new {s.Title, s.Desc}). [difference between Select and SelectMany difference](https://stackoverflow.com/questions/958949/difference-between-select-and-selectmany) – Okan Karadag Apr 01 '22 at 15:37

2 Answers2

2

define the model you need property as follow

public class PostModel
{
    public string Title { get; set; }
    public string Description { get; set; }
}

after you can use it in select statement

InsightsPosts = _db.InsightPosts.Where(p => p.Tags.Contains("test"))
                .Select(s => new PostModel{ Title =s.Title, Description = s.Description })
Okan Karadag
  • 2,542
  • 1
  • 11
  • 23
1

You can create an result model for your method.

public class FullModel
{
    public string PropertyOne { get; set; }
    public string PropertyTwo { get; set; }
}

public class ResultModel
{
    public string PropertyOne { get; set; }
}

then you can use it like this:

var results = lista.Where(p => p.PropertyOne == "one").Select(p => new ResultModel()
{
    PropertyOne = p.PropertyOne,
});

then only result model properties will exists on result

[{"PropertyOne":"one"}]