查询期间如何在Linq对象上设置值

查询期间如何在Linq对象上设置值

本文介绍了查询期间如何在Linq对象上设置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的查询,如下所示

I have a simple query as shown below

var trips = from t in ctx.Trips
            select t;

问题是我需要在Trip对象上分配一个额外的属性,最好不要迭代返回的IQueryable.

The problem is that I have an extra property on the Trip object that needs to be assigned, preferably without iterating through the returned IQueryable.

有人知道如何在查询期间设置值吗? (即选择t,t.property =值")

Does anyone know how to set the value during the query? (i.e. select t, t.property = "value")

推荐答案

首先,如果您从不遍历结果,则您的代码将无法运行. LINQ是惰性"的,这意味着它仅在您需要时才计算下一个结果.

First of all, if you never iterate through the results, your code won't run. LINQ is "lazy", meaning that it only computes the next result when you ask for it.

但是,如果您确实需要做自己想做的事情,请尝试执行以下操作:

However, if you do really need to do what you ask, try something like this:

Trip SetProperty(Trip t)
{
    t.property = "value";
    return t;
}
var trips = from t in ctx.Trips select SetProperty(t);

这篇关于查询期间如何在Linq对象上设置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:56