我在我的代码中使用 LINQ 查询,该查询需要多次编写,在 where 条件下进行微小的更改。我的查询是
var sdata = from r in dt.AsEnumerable()
where r.Field<DateTime>("DAT_START").TimeOfDay.Hours < 20 &&
r.Field<DateTime>("DAT_START").TimeOfDay.Hours >= 4
group r by r["TXT_TARGET_CELL_ID"] into g
select new
{
CellID = g.Key,
TotalCommCount = g.Count(),
TotalDuration = g.Sum(r => r.Field<int>("LNG_DURATION")),
InSMSCount = g.Count(r => r.Field<Int16>("INT_DIRECTION") == 1 &&
r.Field<Int16>("INT_CALL_DATA_TYPE") == 5),
OutSMSCount = g.Count(r => r.Field<Int16>("INT_DIRECTION") == 2 &&
r.Field<Int16>("INT_CALL_DATA_TYPE") == 5),
InVoiceCount = g.Count(r => r.Field<Int16>("INT_DIRECTION") == 1 &&
r.Field<Int16>("INT_CALL_DATA_TYPE") == 1),
OutVoiceCount = g.Count(r => r.Field<Int16>("INT_DIRECTION") == 2 &&
r.Field<Int16>("INT_CALL_DATA_TYPE") == 1),
InVoiceDuration = g.Where(r => r.Field<Int16>("INT_DIRECTION") == 1 &&
r.Field<Int16>("INT_CALL_DATA_TYPE") == 1)
.Sum(r => r.Field<int>("lNG_DURATION")),
OutVoiceDuration = g.Where(r => r.Field<Int16>("INT_DIRECTION") == 2 &&
r.Field<Int16>("INT_CALL_DATA_TYPE") == 1)
.Sum(r => r.Field<int>("LNG_DURATION")),
Latitude = g.Any(s => s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS") != null && s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS").Trim() != "") ? g.First(s => s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS") != null && s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS").Trim() != "").Field<string>("TXT_LATITUDE") : "",
Longitude = g.Any(s => s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS") != null && s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS").Trim() != "") ? g.First(s => s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS") != null && s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS").Trim() != "").Field<string>("TXT_LONGITUDE") : "",
BTS_Address = g.Any(s => s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS") != null && s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS").Trim() != "") ? g.First(s => s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS") != null && s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS").Trim() != "").Field<string>("TXT_TARGET_BTS_LOCATION_ADDRESS") : "",
Azimuth = g.Any(s => s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS") != null && s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS").Trim() != "") ? g.First(s => s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS") != null && s.Field<string>
("TXT_TARGET_BTS_LOCATION_ADDRESS").Trim() != "").Field<string>("TXT_AZIMUTH_DEG") : ""
} into summary
orderby summary.TotalCommCount descending
select summary;
在这里,我只需要每次更改 where 条件,其余部分保持不变,即选择新部分。我可以在代码中编写一次此查询并通过更改 where 条件来调用它吗?