3

How would I write this query in dynamic linq? I know how to do the select, but how do I do the "let"?

var qry = from sr in data.Addresses
                  let AddressType_Desc = sr.AddressType.AddressType_Desc
                  let Country_Desc = sr.Country.Country_Desc
                  where sr.Customer_GUID == CustomerGuid
                  select new
                             {
                                 sr.Address_GUID,
                                 sr.People_GUID,
                                 sr.AddressType_GUID,
                                 AddressType_Desc,
                                 sr.Address1,
                                 sr.Address2,
                                 sr.City,
                                 sr.State,
                                 sr.Zip,
                                 sr.County,
                                 sr.Country_GUID,
                                 Country_Desc,
                                 sr.UseAsMailing
                             };
Jalal
  • 6,594
  • 9
  • 63
  • 100
Jonathan
  • 1,725
  • 3
  • 19
  • 45
  • 1
    What do you mean by "dynamic LINQ". The expression (method) syntax? – mipe34 Feb 10 '13 at 22:36
  • I mean using the System.Linq.Dynamic, to the point I could use a string to define it. – Jonathan Feb 11 '13 at 03:54
  • I guess at that point I might as well just write the t-sql for it. – Jonathan Feb 11 '13 at 03:54
  • See my answer. I have just updated it to fit dynamic LINQ. Don't forget, that you will have to read values of objects queried this way via reflection. An interesting option for creating dynamic linq queries is Predicate builder: http://www.albahari.com/nutshell/predicatebuilder.aspx – mipe34 Feb 11 '13 at 21:00

1 Answers1

4

There is no equivalent of let in linq expression method syntax, as well in dynamic LINQ.

Let can only help you to make your queries more readable. It simply works as an alias or local variable. You can imagine, that in method syntax you won't be able to access it outside the scope of the method declared it.

In your case, just simply put the let variable initiation into the select.

Like this in linq method syntax:

var qry = data.Adresses.Where(sr => sr.Customer_GUID == CustomerGuid)
                       .Select(sr => new {
                                            sr.Address_GUID,
                                            ....

                                            sr.AddressType.AddressType_Desc, 
                                            sr.Country.Country_Desc
                                         });

Or similar with dynamic LINQ (select clause as string):

var qry = data.Adresses.Where(sr => sr.Customer_GUID == CustomerGuid)
                       .Select("new (Address_GUID, AddressType.AddressType_Desc, Country.Country_Desc)");

And you will get the same result as with linq query syntax.

It would be similar for other expression methods. Only thing you need, is to use the value directly instead of the let alias.

mipe34
  • 5,596
  • 3
  • 26
  • 38