Can the code for this example of EF core be written like this?

王好 王 1 Reputation point
2023-11-30T07:57:28.2233333+00:00
    Can the code for this example of EF core be written like this?
public partial class Hlabel
{
    public int Hid { get; set; }

    public string Hname { get; set; } = null!;

    public string Htime { get; set; } = null!;

    public virtual ICollection<Nlabel> Nlabels { get; set; } = new List<Nlabel>();
}

public partial class Nlabel
{
    public int Nid { get; set; }

    public string Nname { get; set; } = null!;

    public string Ntime { get; set; } = null!;

    public int Nhid { get; set; }

    public virtual Hlabel Nh { get; set; } = null!;
}

        
        //Is this return object correct?
        public object SelectGroupLabelList()
        {
            using (var information_context = new InformationContext())
            {
                var query = from hlabel in information_context.Hlabels
                            join nlabel in information_context.Nlabels
                                on hlabel.Hid equals nlabel.Nhid
                            select new {
                                hlabel,
                                nlabel
                            };

                return query.ToList();
            }
        }

        public IActionResult ShowGroupLabelList()
        {
            Hlabel_Service hlabel_service = new Hlabel_Service();
            var list = hlabel_service.SelectGroupLabelList();
            return View(list);
        }
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
779 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,480 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Wenbin Geng 741 Reputation points Microsoft External Staff
    2023-11-30T09:22:12.1766667+00:00

    Hi @王好 王, Welcome to Microsoft Q&A.

    This code looks fine.

            public object SelectGroupLabelList()
            {
                using (var information_context = new InformationContext())
                {
                    var query = from hlabel in information_context.Hlabels
                                join nlabel in information_context.Nlabels
                                    on hlabel.Hid equals nlabel.Nhid
                                select new {
                                    hlabel,
                                    nlabel
                                };
    
                    return query.ToList();
                }
            }
    

    This code returns a list of anonymous type objects, each containing hlabel and nlabel. This anonymous type object is created by the select new { hlabel, nlabel } portion of the LINQ query. In this anonymous type object, each hlabel corresponds to an Hlabel object, and each nlabel corresponds to an Nlabel object.

    You use query.ToList() to convert the query results into a list and ultimately return the list. This way, your SelectGroupLabelList method will return a list containing objects of anonymous type.

    Best Regards,

    Wenbin


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.