how to return result of a linq query in c#?

Farshad Valizade 501 Reputation points
2023-09-26T08:30:10.97+00:00

Hi every body

I have a joined query in linq and I put this query into a function .

I don't know what type should I set instead of void for this function to return data for me.

plz see the code:

        private void GetPaintDetails(byte formId)         {             var query = from f in Paint_FormBLL.GetAll()                         join col in Paint_FormColumnBLL.GetAll() on f.FormId equals col.FormId into fcoll                         from _fcol in fcoll.DefaultIfEmpty()                         join cell in Paint_FormCellBLL.GetAll() on _fcol.ColumnId equals cell.ColumnId into fcell                         from _fcell in fcell.DefaultIfEmpty()                         where f.FormId == formId                         select new                         {                             FormId = f.FormId,                             ColumnId = _fcol.ColumnId,                             ColumnName = _fcol.ColumnName,                             CellId = _fcell.CellId,                             CellValue = _fcell.CellValue                         };         }
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-09-26T09:38:53.2366667+00:00

    Hi @Farshad Valizade , Welcome to Microsoft Q&A,

    Usually we construct a class for the required data and then return its collection: IEnumerable<T> or IQueryable<T>.

    You can also return an object, or the result of a calculation.

    What type is returned usually depends on what you need.

    I wrote an example for you to understand:

    namespace _9_26_x
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
    
        public class PaintDetails
        {
            public byte FormId { get; set; }
            public int? ColumnId { get; set; }
            public string ColumnName { get; set; }
            public int? CellId { get; set; }
            public string CellValue { get; set; }
        }
    
        public class Paint_FormBLL
        {
            public static List<Paint_Form> GetAll()
            {
                // Assume that data is obtained from a database or other data source
                return new List<Paint_Form>
            {
                new Paint_Form { FormId = 1 },
                new Paint_Form { FormId = 2 },
            };
            }
        }
    
        public class Paint_FormColumnBLL
        {
            public static List<Paint_FormColumn> GetAll()
            {
                // Assume that data is obtained from a database or other data source
                return new List<Paint_FormColumn>
            {
                new Paint_FormColumn { ColumnId = 1, FormId = 1, ColumnName = "Column A" },
                new Paint_FormColumn { ColumnId = 2, FormId = 1, ColumnName = "Column B" },
                new Paint_FormColumn { ColumnId = 3, FormId = 2, ColumnName = "Column C" },
            };
            }
        }
    
        public class Paint_FormCellBLL
        {
            public static List<Paint_FormCell> GetAll()
            {
                // Assume that data is obtained from a database or other data source
                return new List<Paint_FormCell>
            {
                new Paint_FormCell { CellId = 1, ColumnId = 1, CellValue = "Value 1" },
                new Paint_FormCell { CellId = 2, ColumnId = 2, CellValue = "Value 2" },
                new Paint_FormCell { CellId = 3, ColumnId = 3, CellValue = "Value 3" },
            };
            }
        }
    
        public class Paint_Form
        {
            public byte FormId { get; set; }
        }
    
        public class Paint_FormColumn
        {
            public int ColumnId { get; set; }
            public byte FormId { get; set; }
            public string ColumnName { get; set; }
        }
    
        public class Paint_FormCell
        {
            public int CellId { get; set; }
            public int ColumnId { get; set; }
            public string CellValue { get; set; }
        }
    
        public class Program
        {
            public static void Main()
            {
                byte formId = 1;
                var paintDetails = GetPaintDetails(formId);
    
                foreach (var detail in paintDetails)
                {
                    Console.WriteLine($"FormId: {detail.FormId}, ColumnId: {detail.ColumnId}, ColumnName: {detail.ColumnName}, CellId: {detail.CellId}, CellValue: {detail.CellValue}");
                }
                Console.ReadLine();
            }
    
            private static IEnumerable<PaintDetails> GetPaintDetails(byte formId)
            {
                var query = from f in Paint_FormBLL.GetAll()
                            join col in Paint_FormColumnBLL.GetAll() on f.FormId equals col.FormId into fcoll
                            from _fcol in fcoll.DefaultIfEmpty()
                            join cell in Paint_FormCellBLL.GetAll() on _fcol?.ColumnId equals cell.ColumnId into fcell
                            from _fcell in fcell.DefaultIfEmpty()
                            where f.FormId == formId
                            select new PaintDetails
                            {
                                FormId = f.FormId,
                                ColumnId = _fcol?.ColumnId,
                                ColumnName = _fcol?.ColumnName,
                                CellId = _fcell?.CellId,
                                CellValue = _fcell?.CellValue
                            };
    
                return query.ToList();
            }
        }
    
    }
    
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    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.


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.