繁体   English   中英

我曾尝试执行此查询,但未能获得预期的(或所需的)结果

[英]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();
            }
        }
    }

创建后,我用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;

    }

在这个地方,我使用事件水龙头进行咨询

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

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

在最后一个代码段中, var pue是作为包含单个字符串属性的匿名对象的枚举而出现的。 MessageBox.Show试图构造一个要显示的字符串,但不知道如何处理该列表。

看来您的意图是提供带有单个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");

现在var pue是一个简单的字符串枚举,并且由于您可能期望一个记录,因此对FirstOrDefault的调用会将枚举从第一个记录转换为单个字符串,如果找不到任何记录,则为null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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