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