jkmills78
8/25/2016 - 3:35 PM

Select data from table using Entity Framework

Select data from table using Entity Framework

using AppModel.DTO;
using AppModel.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;

namespace AppRepository
{
    public class MyRepository : IMyRepository
    {
        private myEntity context;

        public List<MyDTO> GetMyStuff(Guid? guid)
        {
            List<MyDTO> myDTO = new List<MyDTO>();
            using (context = new myEntity())
            {
                var things = from tableOne in context.TableOne
                                 join a in context.TableTwo
                                 on itmtableOne.UniqueID equals a.UniqueID into aGroup
                                 from tableTwo in aGroup
                                 where tableOne.UniqueID == guid
                                 select new
                                 {
                                     tableOne
                                     tableTwo
                                 };

                if (things != null)
                {
                    foreach (var thing in things)
                    {
                        myDTO.Add(new MyDTO()
                        {
                            MyID = thing.tableOne.UniqueID,
                            MyDescription = thing.TableTwo.Description,
                            MyCount = thing.TableOne.Count
                        });
                    }
                    return myDTO;
                }
                else
                    return null;
            }
        }
    }
}