C# 更改查询中的选定对象
我有一个类需要在LINQ到SQL查询中设置属性。我的第一次尝试是使用一个“setter”方法,该方法将返回对象实例,并可在我的select中使用,如下所示:C# 更改查询中的选定对象,c#,linq-to-sql,C#,Linq To Sql,我有一个类需要在LINQ到SQL查询中设置属性。我的第一次尝试是使用一个“setter”方法,该方法将返回对象实例,并可在我的select中使用,如下所示: public partial class Foo { public DateTime RetrievalTime { get; set; } public Foo SetRetrievalTimeAndReturnSelf ( DateTime value ) { RetrievalTime = v
public partial class Foo
{
public DateTime RetrievalTime { get; set; }
public Foo SetRetrievalTimeAndReturnSelf ( DateTime value )
{
RetrievalTime = value;
return this;
}
}
....
from foo in DataContext.GetTable<Foo> select foo.SetRetrievalTimeAndReturnSelf();
公共部分类Foo
{
公共日期时间检索时间{get;set;}
public Foo SetRetrievalTimeAndReturnSelf(日期时间值)
{
检索时间=值;
归还这个;
}
}
....
从DataContext.GetTable中的foo选择foo.SetRetrievalTimeAndReturnSelf();
不幸的是,这样的查询引发如下异常:“System.NotSupportedException:方法'Foo.SetRetrievalTime(System.DateTime)'不支持转换为SQL”
除了将结果转换为列表并对其进行迭代之外,还有其他方法吗?该查询用于包装DataContext.GetTable方法的自定义“Get”方法,因此将用作许多其他查询的基础。立即将可能较大的结果集转换为列表将不是最佳选择
更新
下面是一个更好的例子,用Jason提出的解决方案更新了我正在尝试做的事情:
protected IQueryable<T> Get<T>() where T : class, ISecurable
{
// retrieve all T records and associated security records
var query = from entity in DataContext.GetTable<T> ()
from userEntityAccess in DataContext.GetTable<UserEntityAccess> ()
where userEntityAccess.SysUserId == CurrentUser.Id
&& entity.Id == userEntityAccess.EntityId
&& userEntityAccess.EntityClassName == typeof ( T ).Name
select new { entity, userEntityAccess };
return query.AsEnumerable ()
.Select ( item =>
{
item.entity.CanRead = item.userEntityAccess.CanRead;
item.entity.CanWrite = item.userEntityAccess.CanWrite;
item.entity.CanDelete = item.userEntityAccess.CanDelete;
return item.entity;
} ).AsQueryable ();
}
public interface ISecurable
{
int Id { get; set; }
bool CanRead { get; set; }
bool CanWrite { get; set; }
bool CanDelete { get; set; }
}
protectedIQueryable Get(),其中T:class,ISecurable
{
//检索所有T记录和相关安全记录
var query=来自DataContext.GetTable()中的实体
来自DataContext.GetTable()中的userEntityAccess
其中userEntityAccess.SysUserId==CurrentUser.Id
&&entity.Id==userEntityAccess.EntityId
&&userEntityAccess.EntityClassName==typeof(T).Name
选择new{entity,userEntityAccess};
return query.AsEnumerable()
.选择(项目=>
{
item.entity.CanRead=item.userEntityAccess.CanRead;
item.entity.CanWrite=item.userEntityAccess.CanWrite;
item.entity.CanDelete=item.userEntityAccess.CanDelete;
返回项目。实体;
})。AsQueryable();
}
公共接口不可治愈
{
int Id{get;set;}
布尔可以读取{get;set;}
布尔可以写{get;set;}
布尔坎德莱特{get;set;}
}
UserEntityAccess是用户和业务对象记录(即实体)之间的交叉引用表。每个记录都包含“CanRead”、“CanWrite”和“CanDelete”等字段,并确定特定用户可以对特定记录执行哪些操作
ISecurable是一个标记接口,必须由需要使用此安全Get方法的任何LINQ to SQL域类实现。var projection=DataContext.GetTable
var projection = DataContext.GetTable<Foo>
.AsEnumerable()
.Select(f => f.SetRetrievalTimeAndReturnSelf());
.可计算的()
.Select(f=>f.SetRetrievalTimeAndReturnSelf());
然后,当迭代IEnumerable投影时,这将对DataContext.GetTable中的Foo
的每个实例执行SetRetrievalTimeAndReturnSelf
调用
您需要知道该对象被拉出数据库的时间是什么时候?这可能很难闻。获取“检索”时间只是一个例子。实际上,我将根据查询中的另一条记录计算对象上的设置CanRead/CanWrite属性。我将把这个代码添加到我的原始问题中。我还将尝试您的解决方案并发布结果。