我有MVC 5 C#web API。我将根据系统日期显示交货日计算。我的代码适用于单个子区域,并带有发布日显示的邮政编码(相同的邮政编码可能包含子区域)。任何人都可以帮助我
这是我的送货详细信息屏幕。
这是我的C#代码
int weeklyLogic = 0;
DateTime Firstweek = new DateTime();
DateTime cutDayFirst = new DateTime();
foreach (var delivery in day)
{
weeklyLogic = ((int)Enum.Parse(typeof(DayOfWeek), delivery.DeliveryDay) - (int)today.DayOfWeek + 7) % 7;
var nextweeklyLogic = today.AddDays(weeklyLogic);
Firstweek = nextweeklyLogic;
var cutDayLogic = nextweeklyLogic.AddDays(-delivery.CloseDayId);
cutDayFirst = cutDayLogic;
}
var Location = (from su in db.Suburbs.AsEnumerable()
where su.postcode.Contains(pcode) &&
su.name.Contains(SuburbName)
join sur in db.SubRegions on
su.SubRegionID equals sur.SubregionID
join re in db.AuRegions on
sur.RegionID equals re.RegionID
join DT in db.DeliveryPeriods on
sur.DeliveryTimeId equals DT.Id
select new Posts
{
suburb = new Suburb()
{
name = su.name,
postcode = su.postcode,
},
region = new Region()
{
Name = re.Name
},
deliveryTime = new DeliveryTime()
{
DeliveryDay = DT.DeliveryDay,
DeliveryType = DT.DeliveryType,
},
subRegion = new SubRegion()
{
CloseDayId = sur.CloseDayId,
SubregionName = sur.SubregionName,
SubregionID = sur.SubregionID
},
deliveryDays = new DeliveryDays()
{
Firstweek = Firstweek,
cutDayFirst = cutDayFirst,
},
}).ToList();
return Location1.Select(l => new LocationDTO
{
DeliveryDay = l.deliveryTime.DeliveryDay,
PostCode = l.suburb.postcode,
CloseDayId = l.subRegion.CloseDayId,
SubregionID = l.subRegion.SubregionID,
TFirstDeliveryDay = l.deliveryDays.Firstweek.ToString("dddd, d MMMM yyyy"),
TFirstCutOffDay = l.deliveryDays.cutDayFirst.ToString("dddd, d MMMM yyyy"),
}).ToList();
答案 0 :(得分:1)
我可以通过foreach循环看到你的逻辑是不正确的。 根据你的每个
int weeklyLogic = 0;
DateTime Firstweek = new DateTime();
DateTime cutDayFirst = new DateTime();
foreach (var delivery in day)
{
weeklyLogic = ((int)Enum.Parse(typeof(DayOfWeek), delivery.DeliveryDay) - (int)today.DayOfWeek + 7) % 7;
var nextweeklyLogic = today.AddDays(weeklyLogic);
//initiated for all further processing by last value of day
Firstweek = nextweeklyLogic;
//initiated for all further processing by last value of day
var cutDayLogic = nextweeklyLogic.AddDays(-delivery.CloseDayId);
cutDayFirst = cutDayLogic;
}
通过上述逻辑Firstweek,cutDayFirst始终是foreach循环设置的最后一个,并且这些值对于所有Location都是相同的。
你的整个位置逻辑应该在foreach内部。
<强>更新强>
创建一个新类
public class calculatedDays
{
public int SubRegionID{ get; set; }
public DateTime Firstweekdate { get; set; }
public DateTime cutDayFirstdate { get; set; }
}
并由你的每个逻辑做到这一点。
int weeklyLogic = 0;
DateTime Firstweek = new DateTime();
DateTime cutDayFirst = new DateTime();
List<calculatedDays>Days=new List<calculatedDays>();
foreach (var delivery in day)
{
weeklyLogic = ((int)Enum.Parse(typeof(DayOfWeek), delivery.DeliveryDay) - (int)today.DayOfWeek + 7) % 7;
var nextweeklyLogic = today.AddDays(weeklyLogic);
Firstweek = nextweeklyLogic;
var cutDayLogic = nextweeklyLogic.AddDays(-delivery.CloseDayId);
cutDayFirst = cutDayLogic;
calculatedDays d= new calculatedDays();
d.SubRegionID=delivery.SubRegionID;
d.Firstweekdate =Firstweek ;
d.cutDayFirstdate =cutDayFirst ;
Days.add(d);
}
为您的位置计算添加这些更改。
var Location = (from su in db.Suburbs.AsEnumerable()
where su.postcode.Contains(pcode) &&
su.name.Contains(SuburbName)
join sur in db.SubRegions on
su.SubRegionID equals sur.SubregionID
join re in db.AuRegions on
sur.RegionID equals re.RegionID
join DT in db.DeliveryPeriods on
sur.DeliveryTimeId equals DT.Id
join dts in Days on
su.SubRegionID equals dts.SubRegionID
select new Posts
{
suburb = new Suburb()
{
name = su.name,
postcode = su.postcode,
},
region = new Region()
{
Name = re.Name
},
deliveryTime = new DeliveryTime()
{
DeliveryDay = DT.DeliveryDay,
DeliveryType = DT.DeliveryType,
},
subRegion = new SubRegion()
{
CloseDayId = sur.CloseDayId,
SubregionName = sur.SubregionName,
SubregionID = sur.SubregionID
},
deliveryDays = new DeliveryDays()
{
Firstweek = dts.Firstweekdate ,
cutDayFirst = dts.cutDayFirstdate,
},
}).ToList();
return Location1.Select(l => new LocationDTO
{
DeliveryDay = l.deliveryTime.DeliveryDay,
PostCode = l.suburb.postcode,
CloseDayId = l.subRegion.CloseDayId,
SubregionID = l.subRegion.SubregionID,
TFirstDeliveryDay = l.deliveryDays.Firstweek.ToString("dddd, d MMMM yyyy"),
TFirstCutOffDay = l.deliveryDays.cutDayFirst.ToString("dddd, d MMMM yyyy"),
}).ToList();
希望它会对你有所帮助。谢谢。 :)