c# - Unexpected behaviour with LINQ to Entities, possible bug? -


background
log searches in application , want present ratio between different types of searches in statistics view.

issue
wanted keep efficient , condensed possible in linq still keep clear considering limitations of can use in linq query, attempt this:

(please disregard fact i'm not using separate entity search types, has it's reasons)

    var result = myentities.instance.searchstatistics         .groupby(x => x.searchtype)         .select(y => new list<string> { { y.key }, { sqlfunctions.stringconvert((decimal)y.count()).trim() } })         .tolist();      return json(new     {         text = result.first(x => x.elementat(0) == "text").elementat(1),         organization = result.first(x => x.elementat(0) == "organization").elementat(1),         subject = result.first(x => x.elementat(0) == "subject").elementat(1),     }, jsonrequestbehavior.allowget); 

this behaved in unexpected way though, resulting list in list of lists got it's values in every other row flipped.

i expected {{"text", "123"}, {"organization", "123"}, {"subject", "123"},...}
instead got {{"text", "123"}, {"123", "organization"}, {"subject", "123"},...}

i did not understand why, , tried partition query

    var preresult = myentities.instance.searchstatistics         .groupby(x => x.searchtype).tolist();      var result = preresult         .select(y => new list<string> { { y.key }, { y.count().tostring(cultureinfo.invariantculture) } }).tolist(); 

and worked expected when grouping query executed in db

logging resulting sql of linq query db, got result "faulty" one:

select [project3].[searchtype] [searchtype],        [project3].[c2]         [c1],        [project3].[c1]         [c2]   (select case                  when ([unionall1].[c1] = 0) [groupby1].[k1]                  else ltrim(rtrim(str(cast([groupby1].[a1] decimal(19, 0)))))                end             [c1],                [groupby1].[k1] [searchtype],                1               [c2]           (select [extent1].[searchtype] [k1],                        count(1)               [a1]                   [app].[searchstatistic] [extent1]                 group  [extent1].[searchtype]) [groupby1]                cross join (select 0 [c1]                              (select 1 x) [singlerowtable1]                            union                              select 1 [c1]                              (select 1 x) [singlerowtable2]) [unionall1]) [project3] order  [project3].[searchtype] asc,           [project3].[c2] asc 

unfortunately have limited experience in sql , head hurts looking @ that, getting mixed up.

can pinpoint problem in sql query lies?

is reasonable expect want to? considered bug, or expectations have no support in contract of how linq should work?

update

hold on, why did not @ output sql query directly? mind works in mysterious ways...

the sql query returning :

[searchtype, c1, c2] {text, 1, text}   {text, 1, 123}   {organization, 1, 123}   {organization, 1, organization}   {subject, 1, subject}   {subject, 1, 123}   

but result after .tolist() still containing 3 items. making me more confused, surely must implicit internal behavior of linq have unintended consequences?

it's hard believe order of name , value flipped in json object, apparently happens.

it may have fact order of items in list<t> never guaranteed equal insertion order. can prevent issue not using list @ all:

var result = myentities.instance.searchstatistics     .groupby(x => x.searchtype)     .select(y => new { y.key, count = y.count() })     .tolist();  return json(new {     text = result.first(x => x.key == "text").count.tostring(),     organization = result.first(x => key) == "organization").count.tostring(),     subject = result.first(x => x.key == "subject").count.tostring(), }, jsonrequestbehavior.allowget); 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -