[英]Select items from List of objects that are tuples
I have a class that looks like so; 我有一堂课,看起来像这样;
class MessageEntry
{
public Tuple<bool, int> MessageId;
public Tuple<bool, DateTime> MessageDate { get; internal set; }
public Tuple<bool, String> Subject { get; internal set; }
public Tuple<bool, String> EmailFrom { get; internal set; }
public Tuple<bool, String> EmailTo { get; internal set; }
public Tuple<bool, String> EmailCC { get; internal set; }
public Tuple<bool, String> EmailBCC { get; internal set; }
public Tuple<bool, String> EmailDateSent { get; internal set; }
public Tuple<bool, String> BodyHTML { get; internal set; }
}
I then have a List<MessageEntry>
- List<MessageEntry> test = new List<MessageEntry>
. 然后,我有了一个List<MessageEntry>
List<MessageEntry> test = new List<MessageEntry>
。
How can I select all items frrom test
where Item1
of the tuples is true
? 我如何选择所有test
项目中元组的Item1
为true
?
var result = test.Where(x => x.MessageDate.Item1 && x.Subject.Item1 && ....);
I do not quite understand how you plan to use the code from the question. 我不太了解您打算如何使用问题中的代码。 But I think this code can help you if you change it to your own needs. 但是我认为,如果您根据自己的需要更改此代码,它可以为您提供帮助。
EDIT (I extract check method in extension method, for using in LINQ like style): 编辑(我提取扩展方法中的检查方法,以便在像LINQ的样式中使用):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
var test = new List<MessageEntry>();
var result = test
.Where(entry => entry.IsAllTuplePropertiesIsTrue())
.Aggregate(
string.Empty,
(s, entry) => s + $"value is {entry.IsAllTuplePropertiesIsTrue()}"
);
Console.WriteLine(result);
Console.ReadLine();
}
}
public static class MessageEntryExtension
{
private const BindingFlags BindingFlags =
System.Reflection.BindingFlags.GetProperty
| System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.Instance;
/// <summary>
/// Method check all <see cref="Tuple" /> properties
/// in instance of <see cref="MessageEntry" /> class.
/// </summary>
/// <param name="messageEntry"></param>
/// <returns>If one of several Tuple.Item1 value is not bool false not equal
/// false - return false. Any way - true</returns>
public static bool IsAllTuplePropertiesIsTrue(this MessageEntry messageEntry)
{
var props = typeof (MessageEntry).GetProperties(BindingFlags);
foreach (var value in props.Select(p => p.GetValue(messageEntry)))
{
if (value == null)
return false;
if (typeof (Tuple<,>) == value.GetType())
return false;
var item1Type = value.GetType().GetProperty("Item1", BindingFlags);
var item1Value = item1Type?.GetValue(value);
if (!(item1Value is bool))
return false;
if (!(bool) item1Value)
return false;
}
return true;
}
}
public class MessageEntry
{
public MessageEntry()
{
MessageId = new Tuple<bool, int>(true, 1);
MessageDate = new Tuple<bool, DateTime>(false, DateTime.Now);
Subject = new Tuple<bool, string>(true, "Hello, World!");
}
// Pay attention, I changed this place,
// so as not to duplicate the reflection code.
// For properties and field, it is similar, but still disconnects
public Tuple<bool, int> MessageId { get; internal set; }
public Tuple<bool, DateTime> MessageDate { get; internal set; }
public Tuple<bool, string> Subject { get; internal set; }
public Tuple<bool, string> EmailFrom { get; internal set; }
public Tuple<bool, string> EmailTo { get; internal set; }
public Tuple<bool, string> EmailCC { get; internal set; }
public Tuple<bool, string> EmailBCC { get; internal set; }
public Tuple<bool, string> EmailDateSent { get; internal set; }
public Tuple<bool, string> BodyHTML { get; internal set; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.