【问题标题】:how to get single value by using entity framework如何使用实体框架获取单个值
【发布时间】:2011-08-19 11:26:13
【问题描述】:

我有一个

     table product with colums product_id 
                               prodcut_name
                               category_id

                another table  category 
                               category_id
                               category_name

我正在使用运行良好的 datagridview 填充这些产品详细信息

我需要在 datagridview 中获取所选行的类别名称,因为我已经这样做了......

        private void productGridview_Cellclick(object sender, DataGridViewCellEventArgs e)
        {
               string productname = Convert.ToString(selectedRow.Cells["productnam"].Value);
               var categoryids = from cats in abc.products
                                where cats.product_Name.Equals(productname)
                                select cats.category_Id;


            var catogynames = from categorytypes in abc.categories
                              where categorytypes.category_Name.Equals(categoryids)
                              select categorytypes.category_Name;


            string categorynames = catogynames;    

       }                              

得到一个

  error : cannot implicitly convert type sysytem.linq.iqueryble<string> to string ...

我需要做什么才能在 productgridview productnam 列中获取选定单元格的单个类别名称

任何建议..请..

非常感谢....

修改后的代码: 出错了:

not supported exception:

Unable to create a constant value of type 'System.Object'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

【问题讨论】:

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


    【解决方案1】:

    您的查询正在返回一个行为类似于集合的查询对象。您可以使用FirstFirstOrDefaultSingleSingleOrDefault 分别从查询中检索第一个或单个项目。

    var categoryid = (from cats in abc.products
                      where cats.product_Name.Equals(productname)
                      select cats.category_Id).SingleOrDefault();
    
    
    string categoryname = (from categorytypes in abc.categories
                           where categorytypes.category_Name.Equals(categoryid)
                           select categorytypes.category_Name).SingleOrDefault();
    

    【讨论】:

    • thanq .. 你能再帮我一个忙吗....是否可以通过使用实体和 c# 以编程方式定义映射关系.. 我想以编程方式为一个表指定外键..有可能吗..你有什么想法吗.....
    • 更好的解决方案是使用像select product.Category.category_name 这样的导航属性,将其简化为一个简单的查询。
    【解决方案2】:

    你的问题在这里

      string categorynames = catogynames;
    

    catogynamesIQueryble。你可以这样做,

    List<String> categorynames = catogynames.ToList();
    

    或者你可以遍历catogynames

    P.S 请检查您的变量名称:D.

    【讨论】:

    • 我该怎么做,因为我想将一个值发送到另一个表单取决于 datagridview 单元格点击
    • 在此语句中出现错误 List categories = catogynames.ToList();无法将类型 system.collections.generics.list 隐式转换为 system.collections.generics.list 你能看看这个吗..
    • 将分类名称视为收藏。当然,您可以使用 foreach 遍历它。如果你需要一些单一的值,你可以在没有 foreach 的情况下获得它,只需使用 where 关键字过滤即可。
    • 您收到错误,因为 categorytypes.category_Name 是 char。在这种情况下,您需要 List categorynames = catogynames.ToList();
    猜你喜欢
    • 2014-12-20
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多