嗨! 我有一个数据库模型,其中包含“ receivedMsg”,“ receivedTime”,“ SentBy”等信息
现在,我想将这些信息按数组排序。 如果邮件未满24小时,则应采用今天数组;如果邮件已超过24小时,则应采用周数组。 早于一周的所有内容都应位于更早数组中。
输出应以以下格式显示
row = soup.find('tr') # Extract and return first occurrence of tr
print(row) # Print row with HTML formatting
print("=========Text Result==========")
print(row.get_text()) # Print row as text
请注意,我在C#中使用linq
答案 0 :(得分:0)
我希望,我了解你:
public class RearrangeMessagesInArray
{
public void RearrangeMessagesInArrays(Message[] today, Message[] week, Message[] earlier)
{
// To work with linQ, convert array to list.
var earlierAsList = earlier.ToList();
// Add all messages to "earlier" - array, when its older then 7 days
earlierAsList.ToList().AddRange(GetAllMessagesForGivenArrayAndTime(today, Times.Earlier));
earlierAsList.ToList().AddRange(GetAllMessagesForGivenArrayAndTime(week, Times.Earlier));
//return to "earlier" - array.
earlier = earlierAsList.ToArray();
// Add all messages to "week"-array, which is older than one day and newer than 7 days and remove all which is over 7 days
week = GetAllMessagesForGivenArrayAndTime(week, Times.Week).ToArray();
// Reassign "today"-array without messages, which are older than 1 one day.
today = GetAllMessagesForGivenArrayAndTime(today, Times.Day).ToArray();
PrintOutArrayAsJson(today, week, earlier);
}
private void PrintOutArrayAsJson(Message[] today, Message[] week, Message[] earlier)
{
today.ToList().ForEach(message => Console.WriteLine(WriteFromObject(message)));
week.ToList().ForEach(message => Console.WriteLine(WriteFromObject(message)));
earlier.ToList().ForEach(message => Console.WriteLine(WriteFromObject(message)));
}
private IEnumerable<Message> GetAllMessagesForGivenArrayAndTime(Message[] messages, Times time )
{
switch (time)
{
case Times.Day:
return from message in messages
where GetDaysDiffBetweenNowAndGivenDate(message.ReceivedTime) <= 1
select message;
case Times.Week:
return from message in messages
where GetDaysDiffBetweenNowAndGivenDate(message.ReceivedTime) <= 7
&& GetDaysDiffBetweenNowAndGivenDate(message.ReceivedTime) > 1
select message;
case Times.Earlier:
return from message in messages
where GetDaysDiffBetweenNowAndGivenDate(message.ReceivedTime) > 7
select message;
default:
return null;
}
}
private int GetDaysDiffBetweenNowAndGivenDate(DateTime givenDateTime)
{
return (DateTime.Today - givenDateTime).Days;
}
public class Message
{
public string ReceivedMessage { get; set; }
public DateTime ReceivedTime { get; set; }
public string SentBy { get; set; }
}
private enum Times { Day, Week, Earlier};
// Create a User object and serialize it to a JSON stream.
public string WriteFromObject(Message message)
{
// Create a stream to serialize the object to.
var ms = new MemoryStream();
// Serializer the User object to the stream.
var ser = new DataContractJsonSerializer(typeof(Message));
ser.WriteObject(ms, message);
byte[] json = ms.ToArray();
ms.Close();
return Encoding.UTF8.GetString(json, 0, json.Length);
}
}
答案 1 :(得分:0)
您可以先定义范围,然后使用选择获得所需的结果。 尚未真正检查这些范围是否正确,但是您应该对如何进行了解。参见dotnetfiddle example。
var ranges = new List<GroupRange>
{
new GroupRange
{
Name = "Today",
DateRange = (DateTime.Now.Date, DateTime.Now.AddDays(1).AddTicks(-1))
},
new GroupRange
{
Name = "This Week",
DateRange = (DateTime.Now.FirstDayOfWeek(), DateTime.Now.LastDayOfWeek())
},
new GroupRange
{
Name = "Earlier",
DateRange = (DateTime.MinValue, DateTime.Now.FirstDayOfWeek().AddDays(-1))
},
};
var result = ranges
.Select(r => new
{
Period = r.Name,
Messages = data
.Where(d => d.ReceivedTime >= r.DateRange.Item1 &&
d.ReceivedTime <= r.DateRange.Item2)
});
答案 2 :(得分:0)
难道不应该像这样复杂吗?
class Program
{
static void Main( string[] args )
{
IEnumerable<Item> items = Enumerable.Empty<Item>();
( Age Age, Item[] Items )[] partitioned =
items
.Select( item => ComputeAge(item) )
.GroupBy( x => x.Age )
.Select( g => (
Key : g.Key ,
Values : g.Select( x => x.Item )
.ToArray()
))
.ToArray();
;
}
static ( Item Item, Age Age ) ComputeAge(Item item)
{
double days = (DateTime.Now - item.Received).TotalDays;
Age age;
// Does "week" means "with the last 7 days" or "within the current calendar week"
// We'll assume it means within the last 7 days.
if ( days < 0.0 ) age = Age.Unknown;
else if ( days <= 1.0 ) age = Age.Today;
else if ( days <= 7.0 ) age = Age.Today;
else age = Age.Earlier;
return (item, age) ;
}
}
public enum Age { Unknown = 0, Today = 1, ThisWeek = 2, Earlier = 3 }
class Item
{
public string Message { get; set; }
public string Sender { get; set; }
public DateTime Received { get; set; }
}