score:1

use linq to select the org with the selectedorgname and get the orgid:

pd.cats.insertonsubmit(new category { 
    orgid = pd.orgs.first(o => o.orgname == selectedorgname).orgid, 
    catname = newcatname
});

that assumes selectedorgname will always be in pd.orgs and i'm making that assumption based on the variable name; however, if that isn't always the case, you can do the following:

var selectedorg = pd.orgs.firstordefault(o => o.orgname == selectedorgname);

if (selectedorg != null)
{
    pd.cats.insertonsubmit(new category { 
        orgid = selectedorg.orgid, 
        catname = newcatname
    });
}

score:1

the following is the exact equivalent of what your code does, but it will throw an exception if you ever don't have an org that matches (which you say can't happen):

pd.cats.insertonsubmit(new category { 
    orgid = pd.orgs.last(o => o.orgname == selectedorgname).orgid, 
    catname = newcatname
});

score:2

you can use a int?:

int? selectedorgid = null;

foreach (organization o in pd.orgs)
{
    if (o.orgname == selectedorgname)
    {
         selectedorgid = o.orgid;
    }  
 }

 pd.cats.insertonsubmit(new category 
 { 
        orgid = selectedorgid,
        catname = newcatname 
 });

score:2

edit: op changed the question to specify that only one item will ever be found and a multiple solution isn't needed, here is option 1a

option 1a - one line linq method

this provides a single line linq query that will filter out the unnecessary orgs, grab the single item and select a new category object to be used as the param for your insert. this method will throw an exception if the single item cannot be located, but that is explicitly, what should happen based on your question.

pd.cats.insertonsubmit(
    pd.orgs.where(o=>o.orgname==selectedorgname)
    .single()
    .select(o=>new category { orgid = o.orgid, catname = newcatname })
);

option 1b - iterate a filtered list and perform work

all of the other answers here suggest using linq and assume that only one record will ever be found. why not just do everything in the loop and use linq to filter the results?

foreach (organization o in pd.orgs.where(o=>o.orgname==selectedorgname)) 
{
    pd.cats.insertonsubmit(new category { orgid = o.orgid, catname = newcatname });
}

the benefit here is no if statements and it handles single or multiple cases. there is a way to make this on one line and remove the explicit for each and use list.foreach (see the comparisons):

pd.orgs.where(o=>o.orgname==selectedorgname).tolist()
.foreach(o=>pd.cats.insertonsubmit(new category { orgid = o.orgid, catname = newcatname }));

option 2 - use an exception

this will make it very clear what your code intentions are and let visual studio know that you have this taken care of. the idea is to keep your code very close to the way it is now:

int selectedorgid; 
foreach (organization o in pd.orgs) 
{
    if (o.orgname == selectedorgname) 
       selectedorgid = o.orgid;
}

however, at this point i would suggest you use an exception, such as:

if(selectedorgid == 0) throw new invaliddataexception("selected org id cannot be 0");
pd.cats.insertonsubmit(new category { orgid = selectedorgid, catname = newcatname });

score:2

it's not the only problem. you know you will find 1 or more matches, the compiler doesn't.

but the "or more" is also a problem. the code just isn't clear about what you want, that's the root cause. you have an implicit "last one wins" strategy.

when you use a solution that matches the requirement more closely, the compiler problem goes away without any hacks.

without linq:

// int selectedorgid; 

foreach (organization o in pd.orgs)
    if (o.orgname == selectedorgname)
    {
       pd.cats.insertonsubmit(new category {
         orgid = o.orgid,
         catname = newcatname
      }); 
      return;   // or break;
    }
// shouldn't get here
throw new ...

and with linq

organization org =  pd.orgs.single(o => o.orgname == selectedorgname);
pd.cats.insertonsubmit(new category {
          orgid = selectedorgid,
          catname = newcatname
       }); 

the single(predicate) method closely matches your problem. it will throw when the resultset has != 1 element.

score:3

no.

what happens if the condition o.orgname == selectedorgname is not satisfied for any value in pd.orgs? then selectedorgid will be left uninitialized.

however, the following code might be more 'elegant' according to your approach:

int selectedorgid = pd.orgs.single(o => o.orgname == selectedorgname).orgid;
pd.cats.insertonsubmit(new category { orgid = selectedorgid, catname = newcatname });

please note that your code will set selectedorgid to the last instance of it, while mine will assume that only one exists.

score:4

it appears that you're iterating through a collection, trying to find a single matching value based on the organization name. you can use linq's singleordefault() to find (at most) one match:

var selectedorg = pd.orgs.singleordefault(o => o.orgname == selectedorgname);

then only call insertonsubmit() if a value is found: (otherwise, selectedorg will be null)

if (selectedorg != null)
    pd.cats.insertonsubmit(new category { orgid = selectedorg.orgid, catname = newcatname });

Related Query

More Query from same tag