开发者

getting two column values from same table depending on the condition

开发者 https://www.devze.com 2023-03-29 23:39 出处:网络
I have two tables: table product1 with columns: product_id prodcut_name category_id another tablecategories

I have two tables:

table product1 with columns:
    product_id 
    prodcut_name
    category_id
another table  categories 
    category_id
    category_name
    category_description

I am populating product details using DataGridView and it's working fine.

I am trying to get the two column values from same table depending on the same condition that I have got the code below:

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);

condition 1:

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

condition 2:

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

condition 3:

var categoprydecription = (from categorytable in tsg.categories
                           where categorytable.category_Id开发者_运维问答.Equals(categoryids)
                           select categorytable.category_Description
                          ).SingleOrDefault();

I want to get the categorytypes.category_description also along with this categorytypes.category_Name from the condition 2, is it possible to combine the two conditions? (condition 2 and condition 3)


I think you can do this in this fashion

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

This will be an anonymous class holding both name and description of the categories you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消