c# - Returning a multiple table LINQ query as one object -


so current application need query database using repository returns single object. problem information split 2 different tables. object im trying rule object. rule class consists of following database entities:

class rule     {         stratruledef ruledefinition { get; set; }         list<stratcode> rulecodelist { get; set; } 

in repository im using following query relevant info:

public void getrule(int rulekey)         {             rule rulequery = ruledefinition in arecontext.stratruledefs                             rulecodes in arecontext.stratcodes                             ruledefinition.stratrulekey == rulekey                                    && rulecodes.stratrulekey == rulekey                             select new { ruledefinition, rulecodes }; 

so have 2 problems.

1) how return rulequery 'rule' object, if change 'var' rule following error:

error 10 cannot implicitly convert type 'system.linq.iqueryable' 'testrules.rule'. explicit conversion exists (are missing cast?)

can cast system.linq.iqueryable testrules.rule?

2) tables have 1 many relationship query should return 1 ruledef record , multiple rulecode records, @ moment multiple ruledef records exist duplicates of each other. ideas?

thanks

steve

you have initialize rule object , can use firstordefault if want single object:

ienumerable<rule> rules = ruledefinition in arecontext.stratruledefs                           rulecodes in arecontext.stratcodes                           ruledefinition.stratrulekey == rulekey                               && rulecodes.stratrulekey == rulekey                           select new rule                           {                                ruledefinition = ruledefinition,                                rulecodelist = rulecodes.tolist()                           }; rule firstrule = rules.firstordefault();  // can null if rules empty 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -