【问题标题】:How to Fetch Data from database using Entity Framework 6如何使用 Entity Framework 6 从数据库中获取数据
【发布时间】:2013-11-25 17:43:58
【问题描述】:

我已经构建了一个查询来返回两个表中的数据,它们通过内部联接进行联接。虽然,由于查询看起来不错,但当我尝试从查询中访问选定的字段名称时收到错误消息。我如何在此查询中使用 .SingleOrDefault() 函数。任何人都可以帮助我如何进行。

private void FindByPincode(int iPincode)
    {
        using (ABCEntities ctx = new ABCEntities())
        {
            var query = from c in ctx.Cities
                        join s in ctx.States
                        on c.StateId equals s.StateId
                        where c.Pincode == iPincode
                        select new {
                                s.StateName, 
                                c.CityName, 
                                c.Area};

            // var query = ctx.Cities.AsNoTracking().SingleOrDefault(_city => _city.Pincode == iPincode);

            if (query != null)
            {
                cboState.SelectedItem.Text =query.State;        //Getting error "Could not found"
                cboCity.SelectedItem.Text = query.CityName;     //Getting error "Could not found"
                txtArea.Text = query.Area;                        //Getting error "Could not found"

            }

        }
    }

提前致谢。

【问题讨论】:

  • 错误是“'System.Linq.IQueryable' 不包含'StateName' 的定义并且没有扩展方法'StateName' 接受'System.Linq 类型的第一个参数。可以找到 IQueryable'(您是否缺少 using 指令或程序集引用?)"

标签: c# entity-framework entity-framework-4 linq-to-entities


【解决方案1】:

试试这个:

using (ABCEntities ctx = new ABCEntities())
    {
        var query = (from c in ctx.Cities
                    join s in ctx.States
                    on c.StateId equals s.StateId
                    where c.Pincode == iPincode
                    select new {
                            s.StateName, 
                            c.CityName, 
                            c.Area}).FirstOrDefault();

        if (query != null)
        {
            cboState.SelectedItem.Text =query.State;        
            cboCity.SelectedItem.Text = query.CityName;    
            txtArea.Text = query.Area;                        
        }
    }

【讨论】:

  • 我看到了你的代码,但不明白你为什么使用 .First() 和 SingleOrDefault() 来获取数据.......First().SingleOrDefault(); 请告诉我原因吗?
【解决方案2】:

是不是您正在选择一个名为 StateName 的字段,然后访问 State。

 cboState.SelectedItem.Text =query.State;    



cboState.SelectedItem.Text =query.StateName;    

请提供有关错误和代码类结构的更多详细信息

【讨论】:

    【解决方案3】:

    以下是类似情况对我的影响:

    using (ABCEntities ctx = new ABCEntities())
    {
            var query = (from c in ctx.Cities
                        join s in ctx.States
                        on c.StateId equals s.StateId
                        where c.Pincode == iPincode
                        select new {
                                s.StateName, 
                                c.CityName, 
                                c.Area}).Single();
    
            if (query.Any())
            {
                cboState.SelectedItem.Text =query.State;
                cboCity.SelectedItem.Text = query.CityName;
                txtArea.Text = query.Area;
            }
    }
    

    请注意,我在那里使用了 query.Any(),这是因为 query != null 将始终返回 true。但是 any() 检查查询是否返回了 1 条或更多记录,如果是,则 Any() 返回 true,如果查询没有返回记录,则 Any() 返回 false。

    【讨论】:

    • 如果不是 single(),查询返回默认值怎么办。然后它会变成一个错误/异常。
    • @SorangwalaAbbasali 在这种情况下,您可以使用 SingleOrDefault()。
    猜你喜欢
    • 2017-01-23
    • 1970-01-01
    • 2013-12-16
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    相关资源
    最近更新 更多