Dynamic Linq Conditional Select

c# dynamic dynamic-linq linq

Question

var Data = from z in initialData
           select new
           {
              z.ID,
              z.Value = (z.Col1 != null)? z.Col1 : z.Col2
           };

How can I convert this query to a dynamic linq expression? Is this even possible?

1
1
4/15/2013 12:19:16 PM

Popular Answer

try this http://msdn.microsoft.com/en-us/library/bb345303.aspx

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Call the constructor with a query for products and the ObjectContext.
    ObjectQuery<Product> productQuery1 =
        new ObjectQuery<Product>("Products", context);

    foreach (Product result in productQuery1)
        Console.WriteLine("Product Name: {0}", result.Name);

    string queryString =
        @"SELECT VALUE product FROM AdventureWorksEntities.Products AS product";

    // Call the constructor with the specified query and the ObjectContext.
    ObjectQuery<Product> productQuery2 =
        new ObjectQuery<Product>(queryString, context);

    foreach (Product result in productQuery2)
        Console.WriteLine("Product Name: {0}", result.Name);

    // Call the constructor with the specified query, the ObjectContext,  // and the NoTracking merge option.
    ObjectQuery<Product> productQuery3 =
        new ObjectQuery<Product>(queryString,
            context, MergeOption.NoTracking);

    foreach (Product result in productQuery3)
        Console.WriteLine("Product Name: {0}", result.Name);
}

you might take a look at this section

string queryString =
    @"SELECT VALUE product FROM AdventureWorksEntities.Products AS product";

// Call the constructor with the specified query and the ObjectContext.
ObjectQuery<Product> productQuery2 =
    new ObjectQuery<Product>(queryString, context);

foreach (Product result in productQuery2)
    Console.WriteLine("Product Name: {0}", result.Name);

don't forget to mark it answered if it works for you.

0
11/15/2012 11:44:34 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow