1
是否可以在LINQ
查询中为SELECT
制作模板?现在我有6
方法使用完全相同的SELECT,我想尽可能使用模板。选择模板
这是我正在使用的代码,当我想对选择进行更改时,我必须在代码中的许多位置更改相同的内容。
result = query.Select(b => new
{
route_id = b.b.route_id,
name = b.b.name,
description = b.b.description,
distance = b.b.distance,
distance_to_route = (int)b.distance_to_from_me,
departure_place = b.b.departure_place,
arrival_place = b.b.arrival_place,
owner = b.b.user.username,
average_rating = b.avg_rating,
is_favorite = b.is_favorite,
date = b.b.date,
attributes = b.b.route_attributes.Select(c =>
c.route_attribute_types.attribute_name),
coordinates = b.b.coordinates.Select(c =>
new coordinateToSend { sequence = c.sequence,
lat = c.position.Latitude,
lon = c.position.Longitude })
});
为什么不使用具有适当构造函数的类? – 2013-03-13 10:25:41
尝试一些像AutoMapper的mappers,但也与Tim的建议 – TalentTuner 2013-03-13 10:28:10
所以我可以创建一个类为我创建选择? – user2049921 2013-03-13 10:31:45