0

Please help me, I am already stucked, to get the value (object) item of array list. which my array list filled from query . And the query result is in entity framework 6.0 .ToList() function . thank you very much for your help

private List<Documents> getDocOwn(string sortOrder, string searchStringName, string searchStringDept)
    {
        try
        {
            DocumentOwnerMapper objDocOwnMapper = new DocumentOwnerMapper();
            var listDocOwn = objDocOwnMapper.getList(sortOrder, searchStringName, searchStringDept);
            var listDocOwnModel = new List<M_DC_DocumentOwnerModel>();
            foreach (var obj in listDocOwn)
            {
               //how i can get the value of obj [dept] / obj[Name]
            }
            return new List<Document>();
        }
        catch (Exception ex)
        {
            return new List<Document>();
        }
    }

and here my public class for querying into database

public ArrayList getList(string strSortOrder, string strSeacrhStringName, string strSearchDept)
    {
        try
        {
            using (Entities ent = new Entities ())
            {
                ArrayList arrObj = new ArrayList();
                if (!string.IsNullOrEmpty(strSortOrder))
                {
                                                    var listObjName = ent.DocumentOwner.Join
                                 (ent.Employee.Where(o => o.Active == true).OrderBy(o => o.NAME),
                                 docown => docown.EMP_ID,
                                 EAL => EAL.EMP_ID,
                                 (docown, EAL) => new
                                 {
                                     NIK = docown.EMP_ID,
                                     Name = EAL.NAME,
                                     Dept = EAL.Organiz_Stru,
                                     EmailTo = docown.EmailAddressTo,
                                     EmailCC = docown.EmailAddressCC,
                                     EmailBCC = docown.EmailAddressBCC,
                                     ActiveEmp = EAL.Active
                                 }).ToList();

                            foreach (var obj in listObjName)
                            {
                                arrObj.Add(obj);
                            }
                    
                
                return arrObj;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return new ArrayList();
    }
7
  • without seeing any code it's pretty hard to hekp help you. Apart from this ToList doesn't return an ArraList, but a List<T>. Commented Oct 28, 2021 at 13:08
  • Could you provide more code? query.toList() will create a List out of your IEnumerable. query.First() gets you the first object of your query, query.FirstOrDefault() is also a possibility if you want a default if there is an empty query. If you want to get a specific item out of your query you should use query.Where().First() and add your filter criteria into the Where method. Commented Oct 28, 2021 at 13:12
  • and where exactly do you try to access the arrays elements? Please be more specific on your issue. Commented Oct 28, 2021 at 13:12
  • @Yingrjimsch... yeeahh.. i figured out how to attach my code. and i already modified my question.. please help guys. thanks Commented Oct 28, 2021 at 13:26
  • 1
    @HimBromBeere ... yeeahh.. i figured out how to attach my code. and i already modified my question.. please help guys. thanks Commented Oct 28, 2021 at 13:26

1 Answer 1

1

yeeyy alhamdulillah...

by helping from my friend, I have figured out the solution. like below

foreach (var obj in listDocOwn)
        {
           var objDocOwnMod = new M_DC_DocumentOwnerModel()
                {
                    Name = (obj.GetType().GetProperty("Name").GetValue(obj)).ToString(),
                          Dept = (obj.GetType().GetProperty("Dept").GetValue(obj)).ToString()
               };
                listDocOwnModel.Add(objDocOwnMod);
        }
Sign up to request clarification or add additional context in comments.

2 Comments

great if you found the solution. I wa thinking maybe if you are creating a => new MyClass instead of => new the type is defined and you can get dept by obj.Dept In my opinion the lack of typing leads to insufficient informations.
@Yingrjimsch .. actually, before i found this the solution.. i want to take out the classes in model folder.. and that classes can i use for customize query result which is not suitable with my entity context (DB first). if there is no solution for me. i will take this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.