【问题标题】:getting two column values from same table depending on the condition根据条件从同一个表中获取两个列值
【发布时间】:2011-08-22 10:22:17
【问题描述】:

我有两张桌子:

表 product1 与列: product_id 产品名称 类别ID 另一个表类别 类别ID 分类名称 类别描述

我正在使用 DataGridView 填充产品详细信息,它工作正常。

我正在尝试根据我获得以下代码的相同条件从同一个表中获取两列值:

string desc = Convert.ToString(selectedRow.Cells["productdescr"].Value);
string productname = Convert.ToString(selectedRow.Cells["productnam"].Value);
string productprices = Convert.ToString(selectedRow.Cells["productprice"].Value);
int  productids = Convert.ToInt32(selectedRow.Cells["productid"].Value);

条件 1:

int  categoryids = (from cats in tsg.product1
                    where cats.product_Name.Equals(productname)
                    select cats.category_Id).SingleOrDefault();

条件 2:

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

条件 3:

var categoprydecription = (from categorytable in tsg.categories
                           where categorytable.category_Id.Equals(categoryids)
                           select categorytable.category_Description
                          ).SingleOrDefault();

我想从 条件 2 中同时获得 categorytypes.category_descriptioncategorytypes.category_Name,是否可以将这两个条件结合起来? (条件2和条件3)

【问题讨论】:

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


    【解决方案1】:

    我认为你可以这样做

    (from categorytable in tsg.categories
    where categorytable.category_Id == categoryids
    select new {Name=categorytable.category_Name, 
                Description=categorytable.category_Description}).SingleOrDefault();
    

    这将是一个匿名类,包含您想要的类别的名称和描述。

    【讨论】:

    • 将where条件改为categorytable.category_Id == categoryids
    猜你喜欢
    • 2018-01-20
    • 2019-07-26
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 2014-02-18
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多