English 中文(简体)
2. 如何在林盖内引爆物体
原标题:How to instantiate an object within a linq query

This is kinda theoretical question, I was looking at someone else code (below) and my simple solution was to instantiate the collection outside linq, but I can guess there will be cases where I d want to instantiate the objects inside the query, and perhaps only on a selection of elements. Here s a simplified example of how this was being done (badly).

var pods = (from n in ids
where new Node(Convert.ToInt32(n)).HasValue("propertyName") 
select new
{
    Id = Convert.ToInt32(n),
    Url = new Node(Convert.ToInt32(n)).Url,
    Name = new Node(Convert.ToInt32(n)).Title()
}).ToList();

无关的说明:在这种情况下,诺德建筑商正在从一个记忆库获取数据。

我怎样才能改进这一榜样,在每件物体使用lin子时才能即时发射?

Cheers. Murray.

最佳回答

使用<条码> 类似条款:

var pods = (
    from n in ids
    let id = Convert.ToInt32(n)
    let node = new Node(id)
    where node.HasValue("propertyName") 
    select new
    {
        Id = id,
        Url = node.Url,
        Name = node.Title()
    }
).ToList();

详情请见let Clause (C# Reference):

In a query expression, it is sometimes useful to store the result of a sub-expression in order to use it in subsequent clauses. You can do this with the let keyword, which creates a new range variable and initializes it with the result of the expression you supply. Once initialized with a value, the range variable cannot be used to store another value. However, if the range variable holds a queryable type, it can be queried.

问题回答

暂无回答




相关问题
LINQ to SQL and a running total on ordered results

I want to display a customer s accounting history in a DataGridView and I want to have a column that displays the running total for their balance. The old way I did this was by getting the data, ...

Can I do this with an IQueryable<T>?

is it possible to add an extension method to IQueryable<T> and then, for my Linq2Sql or EF or NHibernate or LinqToObjects, define it s functionality / how each repository will impliment this ...

AlphaNumeric ordering in SQL vs. LINQ and .NET

I encountered an interesting thing today that I have never noticed before. It appears that SQL and LINQ order AlphaNumeric strings differently. Data table contains rows: A G 6 P 1 D J 2 T Z 9 F 0 ...

If statement or Where extension with a For Each loop?

I was wondering which of the two code samples would be more efficient (or is the difference between the two negligible)? For Each apple in AppleController.GetRedApples().Where(Function(a) ...

Differences between LINQ to Objects and LINQ to SQL queries

I have been using LINQ to query my POCO objects for some time, but I have not yet tried LINQ to SQL. I assume that LINQ to SQL queries are somehow converted to equivalent SQL queries and, given this, ...

动态准则 询问

能否在业余时间建立LinqQuries。

热门标签