question

bushra-8117 avatar image
0 Votes"
bushra-8117 asked OlafHelper-2800 commented

i want to dynamically assign a string which i got from my above output as the left hand side assignment in linq

var weeklyutilitylist1 = weeklyutilitylist.GroupBy(x => new
{

             x.asset_type,


              
         })
                
            
               .Select(x => new
               {

                   Asset_type =x.Key.asset_type,


                  i want asset_type here = x.OrderBy(x => x.week_num_of_month)
                     
               }) 
               .ToList();
            

         return Ok(weeklyutilitylist1);
sql-server-generaldotnet-csharpdotnet-aspnet-core-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You tagged your post with sql server; in which way is it related to? It isn't, so remove the tag.

0 Votes 0 ·

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered cooldadtx edited

You can't. You're using an anonymous type in your select which means the member names are fixed at the point of compilation. It would be no different than you using a named type. All an anonymous type really does is cause the compiler to auto-generate the actual type instead of you explicitly declaring it. Since C# doesn't support "adding members" at runtime you cannot do it this way.

There are some alternatives that can get you closer to what you want. Honestly is the name that important? If you cannot come up with a good name then use something generic like Value. It would make this whole problem go away. But if you really want this behavior then you have a couple of options but they aren't pretty.

1) Use a dynamic object instead. This makes coding against that structure very, very unpleasant and pretty much prevents any useful compiler checking that normally happens. If you're using a dynamic type, for example, the compiler will gladly compile anything after the member access (`mydynamic.nonexistentmember`) and it won't be until runtime you find out it is broken.

Additionally you're playing with LINQ here and not all LINQ providers may allow you to use dynamic. Ensure you test your code against the actual LINQ provider (EF, memory, etc) you want to use otherwise it'll fail.
2) Similar is to use ExpandoObject. It is basically the same thing but slightly cleaner. You still don't get compiler checking.
3) Use a dictionary such that the "property" becomes the key and the value is the value. This is probably the cleanest approach for this specific scenario.
4) A final thought which may or may not work for your particular case is to let the compiler figure it out. In most cases you probably specify the "property" name when using an anonymous type. But if you leave the name off then the compiler auto-generates one based upon the right side value. If you are trying to build something reusable then this may give you what you need but it would be very, very narrow use case.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.