[英]Dynamic Linq Conditional Select
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? 如何将该查询转换为动态linq表达式? Is this even possible? 这有可能吗?
try this http://msdn.microsoft.com/en-us/library/bb345303.aspx 试试这个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. 如果适合您,请不要忘记将其标记为已回答。
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. 编辑:没关系,误读了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.