Casting issue in LINQ (C# .NET 4) -


i have come across confusing problem hope can me with. in application have following data structures:

public struct entitydetails {     public string entityname { get; set; }     public list<attributedetails> attributes { get; set; }     public bool entityexists { get; set; } }  public struct attributedetails {     public string attributename { get; set; }     public string displayname { get; set; }     public string datatype { get; set; }     public string description { get; set; }     public bool attributeexists { get; set; } } 

i instantiate object following:

public static list<entitydetails> entitylist { get; set; } 

so, need able return filtered list of attributes based on entity name , attribute name. wrote following piece of linq:

public static list<attributedetails> getfilteredattributelist(string pentityname, string pattributefilter) {        return (list<attributedetails>)entitylist.where(e => e.entityname == pentityname)                                              .select(e => e.attributes                                                            .where (a => a.attributename                                                            .contains (pattributefilter)));    } 

initially, when did didn't have cast @ start, brought compile time error, added cast allow compile. however, when gets method following message:

{"unable cast object of type 'whereselectlistiterator2[mpya.bu.crmclientupdatetool.crmaccess.crmaccesslayer+entitydetails,system.collections.generic.ienumerable1[mpya.bu.crmclientupdatetool.crmaccess.crmaccesslayer+attributedetails]]' type 'system.collections.generic.list`1[mpya.bu.crmclientupdatetool.crmaccess.crmaccesslayer+attributedetails]'."}

now, research i've done appear 1 of type ienumerable , other list, understand, can't life of me work out how cast acceptable! i've tried tolist(), casting through extension methods , various other things. i've confirmed data structure contains correct data.

any appreciated.

update

apologies, reason can't reply answers 8 hrs sigh. have followed advice of use tolist , following error:

thanks answers far. in mind tolist() logical way go, when following compile-time error:

error cs0029: cannot implicitly convert type 'system.collections.generic.list<system.collections.generic.ienumerable<mpya.bu.crmclientupdatetool.crmaccess.crmaccesslayer.attributedetails>>' 'system.collections.generic.list<mpya.bu.crmclientupdatetool.crmaccess.crmaccesslayer.attributedetails>' 

the actual error message when hover on "system.argumentnullexception".

you can end linq query call .tolist() convert results list<t>.

one thing keep in mind calling .tolist() on linq query "realizes" it, meaning execution no longer deferred;the results stored in memory in list<t>.

in addition, believe want use .selectmany() clause, instead of .select(). e.attributes list<attributedetails>. if use select(), create ienumarable<list<attributedetails>>, each element being attributes 1 of entities. selectmany combine returned lists , return ienumerable<attributedetails>, appears want.

ultimately, want use following:

return entitylist.where(e => e.entityname == pentityname)                  .selectmany(e => e.attributes                              .where (a => a.attributename                                      .contains(pattributefilter)))                  .tolist(); 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -