简体   繁体   中英

How do I send a Object Linq sorted List as a parameter

I am trying to make my code more compromised, and use overall less, however currently I'm running into the problem of not being able to send a list of Objects sorted by linq as a parameter. the problem is in this part of the code:

List<Afspraken> dataAfspraken = new List<Afspraken>();

public Form1()
    {
        InitializeComponent();
        fillListsForLinq();
        loadReceptionData();
    }

        private void fillListsForLinq()
        {
            dataAfspraken = data.getAfsprakenData();
            //here it fills the list with Afspraken objects
        }

private void loadReceptionData()
    {
        private void loadReceptionGrid
            var receptionToFinnish =
                (from AFspraken in dataAfspraken
                 where Afspraken.factuur_betaald == true && Afspraken.volledig_afgerond == false
                 join Users in dataUsers on Afspraken.gekoppelde_klant equals Users.id
                 select new
                 {
                     Id = Afspraken.id,
                     Klant = Users.gebruikersnaam,
                     Betaald = Afspraken.factuur_betaald,
                     Afgerond = Afspraken.volledig_afgerond
                 }).ToList();
            changeDataviewReception(receptionToFinnish);
        }

        private void changeDataviewReception(List<Object> listData)
        {
            dgvReceptionData.DataSource = listData
        }

the Afspraken class looks like this

        public class Afspraken
    {
       
        public int id { get; set; }
        public bool bevestigd { get; set; }
        public DateTime datum { get; set; }
        public int gekoppelde_klant { get; set; }
        public int gekoppelde_monteur { get; set; }
        public string benodigde_hadelingen { get; set; }
        public decimal totaalprijs { get; set; }
        public bool klaar { get; set; }
        public bool factuur_betaald { get; set; }
        public bool volledig_afgerond { get; set; }
        public string opmerkingen { get; set; }
    }

How do I get receptionToFinnish as a parameter into changeDataviewReception ?

receptionToFinnish will be a list full of objects of an anonymous type. But your method requires a List<object> . This is now allowed since a list is not a variant type.

Say for example that you have a list of bananas and want to give it to someone that wants a list of fruits. This will not work since that other person might try to add an orange to the list of bananas.

To fix this, cast the values to object explicitly, for example:

select new
{
    Id = Afspraken.id,
    Klant = Users.gebruikersnaam,
    Betaald = Afspraken.factuur_betaald,
    Afgerond = Afspraken.volledig_afgerond
} as object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM