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

如何将此查询转换为动态 linq 表达式?这甚至可能吗?

4

2 回答 2

0

试试这个 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);
}

你可以看看这个部分

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);

如果它对您有用,请不要忘记将其标记为已回答。

于 2012-11-15T11:44:34.857 回答
-1

You are missing member names for your anonymous type. Included in my answer:

var Data = initialData.Select(x => new 
                           { 
                               ID = x.ID, 
                               Value = (x.Col1 == null)? x.Col1 : x.Col2
                           });

EDIT: Nevermind, misread the question.

于 2012-11-15T11:26:31.323 回答