简体   繁体   中英

i've tried doing this query but haven't been able to get the expected (or desired) result

i've tried doing this query but haven't been able to get the expected (or desired) result

        using (ModelDBDataContext dataContext = new ModelDBDataContext("Data Source='isostore:/PueblosDB.sdf'"))
        {
            if (!dataContext.DatabaseExists())
            {
                dataContext.CreateDatabase();

                List<Pueblo> puebloObj = new List<Pueblo>()
                {
                    new Pueblo()
                    {
                    Id=1, Nombre="Aguadas", Imagen="Recursos/Imagenes/aguadas.png",Descrip="Algo",Coord="Algo"
                    },

                    new Pueblo()
                    {
                    Id=2, Nombre="Villa de Leyva", Imagen="Recursos/Imagenes/Villa de Leyva.png",Descrip="algo",Coord="Algo"
                    }
                 };

                dataContext.Pueblo.InsertAllOnSubmit(puebloObj);
                dataContext.SubmitChanges();
            }
        }
    }

After create i join the data with the xaml

    public List<Pueblo> misPueblos { get; set; }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        using (ModelDBDataContext contextoDatos = new ModelDBDataContext("Data Source='isostore:/PueblosDB.sdf'"))
        {
            misPueblos = contextoDatos.Pueblo.ToList();
        }

        listBox2.ItemsSource = misPueblos;

    }

In this place i do the consult using the event tap

        var pue = from Pueblo in misPueblos
                  where Pueblo.Id == Convert.ToInt32(id)
                  select (new{Pueblo.Nombre}) ;

        MessageBox.Show("este es el id: " + pue.ToList());

in your final snippet, var pue is coming out as an enumeration of anonymous objects containing a single string property. MessageBox.Show is trying to construct a string to display but doesn't know what to do with that list.

It appears that your intention is to get a single string property provided a single id.

    var pue = from Pueblo in misPueblos
              where Pueblo.Id == Convert.ToInt32(id)
              select Pueblo.Nombre ;

    MessageBox.Show("este es el id: " + pue.FirstOrDefault()??"Not Found");

now var pue is a simple enumeration of strings, and since you're probably expecting one record, the call to FirstOrDefault converts the enumeration into a single string from the first record, or null if no records found.

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