简体   繁体   English

Linq查询以使用列表列表来过滤列表

[英]Linq query to use a list of lists to filter a list

I'm still learning LINQ, and I'm attempting to use an object from one list as an identifier to find objects in another list. 我仍在学习LINQ,并且尝试使用一个列表中的对象作为标识符来查找另一个列表中的对象。

I have two lists of objects, similar to my mock-up code below. 我有两个对象列表,类似于下面的模拟代码。 Using LINQ, I want to list all Colors.Hex of a specific Country's flag color, using the variable selectedCountry. 我想使用LINQ使用变量selectedCountry列出特定国家/地区的标志颜色的所有Colors.Hex。

So if selectedCountry = "USA", I would like to be able to console.write: 因此,如果selectedCountry =“ USA”,我希望能够console.write:

USA
RED FF0000
Blue 0000FF
White FFFFFF

Preferably in query syntax, for readability. 为了可读性,最好在查询语法中。

    public class Countries
{
    public string Name;
    public List<string> FlagColors = new List<string>();
}

public class Colors
{
    public string Name;
    public string Hex;
}

public partial class Form1 : Form
{
    public static List<Countries> country = new List<Countries>();
    public static List<Colors> color = new List<Colors>();

    public void foo()
    {
        color.Add(new Colors { Name= "Red", Hex = "FF0000"});
        color.Add(new Colors { Name= "Blue", Hex = "0000FF" });
        color.Add(new Colors { Name= "White", Hex = "FFFFFF" });
        color.Add(new Colors { Name= "Yellow", Hex = "FFFF00" });

        Countries newCountry = new Countries();
        newCountry.Name = "USA";
        newCountry.FlagColors.Add("Red");
        newCountry.FlagColors.Add("White");
        newCountry.FlagColors.Add("Blue");
        country.Add(newCountry);

        Countries newCountry2 = new Countries();
        newCountry2.Name = "Sweden";
        newCountry2.FlagColors.Add("Blue");
        newCountry2.FlagColors.Add("Yellow");
        country.Add(newCountry2);

        string selectedCountry = "USA";

        // Linq query here
    }
}

Thanks in advance 提前致谢

You can do this like so: 您可以这样做:

Country selectedCountry = country.SingleOrDefault(x => x.Name == selectedCountry);
if (selectedCountry != null) {
    Console.WriteLine(selectedCountry.Name);
    foreach (string flagColor in selectedCountry.FlagColors) {
        Colors color = color.SingleOrDefault(x => x.Name == flagColor);
        if (color != null) {
            Console.WriteLine(color.Name + " " + color.Hex);
        }
    }
}

As you can see the LiNQ queries are pretty simple, you basically want to return the first element that matches the condition predicate (in this case, where the Name is equal to the selectedCountry or the flagColor . 如您所见,LiNQ查询非常简单,您基本上想返回与条件谓词匹配的第一个元素(在这种情况下, Name等于selectedCountryflagColor

Something like this, perhaps: 大概是这样的:

    var q = from c1 in country
        from c2 in c1.FlagColors
        from c3 in color
        where c3.Name == c2 && c1.Name == selectedCountry
        select c3.Hex;

Or alternatively: 或者:

    var q = from c1 in country
        from c2 in c1.FlagColors
        join c3 in color on c2 equals c3.Name
        where c1.Name == selectedCountry
        select c3.Hex;

Linq 可以使用 DateTime 列表过滤 List 的查询<object><div id="text_translate"><p>我正在尝试通过使用动态创建的 DateTimes 列表,在任何周末过滤一组数据。 DateTimes的列表中有几个日期,其中包含一个<strong>开始日期</strong>和一个<strong>结束日期</strong>。 这可以是过去的任何周末。</p><p> DateTimes 列表类似于以下内容:</p><pre> public class ShiftTimeFilter { public DateTime ShiftTimeFrom { get; set; } public DateTime ShiftTimeTo { get; set; } }</pre><p> 并且创建的日期在new List<ShiftTimeFilter>(); 这会为给定的周末创建以下示例日期。</p><pre> [0] ShiftTimeFrom: {23/01/2021 00:30}, ShiftTimeTo: {23/01/2021 16:30} [1] ShiftTimeFrom: {24/01/2021 17:30}, ShiftTimeTo: {24/01/2021 19:30}</pre><p> 我可以做一天,即周六或周日,但我无法理解如何使用 DateTimes 列表将这两天合并到一个 go 中进行过滤。myData model 如下:</p><pre> public class myData { public DateTime ReadingDate { get; set; } public double ReadingValue { get; set; } }</pre><p> 我正在使用的查询:</p><pre> var data = myData.Where(x => x.ReadingDate.DayOfWeek == DayOfWeek.Saturday && x.ReadingDate > ShiftTimeFrom && x.ReadingDate <= ShiftTimeFrom ).Select(x => new Data { myDate = x.ReadingDate, myVal = x.ReadingValue }).ToList();</pre><p> 我遇到的问题之一是,客户可能会选择几个月的时间段,他们希望在这段时间的每个周末都看到。 因此,在某些情况下,日期列表可以包含 10 或 20 个日期。</p><p> 我希望使用 Linq 来过滤日期,给我所有日期之间的所有数据List<ShiftTimeFilter>; 以及那些不在这些日期之间的任何东西,我想将它的值设置为零。</p><p> TIA</p></div></object> - Linq query that can use a list of DateTime to filter a List<object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ查询返回列表列表 - LINQ Query Returns List of Lists Linq 可以使用 DateTime 列表过滤 List 的查询<object><div id="text_translate"><p>我正在尝试通过使用动态创建的 DateTimes 列表,在任何周末过滤一组数据。 DateTimes的列表中有几个日期,其中包含一个<strong>开始日期</strong>和一个<strong>结束日期</strong>。 这可以是过去的任何周末。</p><p> DateTimes 列表类似于以下内容:</p><pre> public class ShiftTimeFilter { public DateTime ShiftTimeFrom { get; set; } public DateTime ShiftTimeTo { get; set; } }</pre><p> 并且创建的日期在new List<ShiftTimeFilter>(); 这会为给定的周末创建以下示例日期。</p><pre> [0] ShiftTimeFrom: {23/01/2021 00:30}, ShiftTimeTo: {23/01/2021 16:30} [1] ShiftTimeFrom: {24/01/2021 17:30}, ShiftTimeTo: {24/01/2021 19:30}</pre><p> 我可以做一天,即周六或周日,但我无法理解如何使用 DateTimes 列表将这两天合并到一个 go 中进行过滤。myData model 如下:</p><pre> public class myData { public DateTime ReadingDate { get; set; } public double ReadingValue { get; set; } }</pre><p> 我正在使用的查询:</p><pre> var data = myData.Where(x => x.ReadingDate.DayOfWeek == DayOfWeek.Saturday && x.ReadingDate > ShiftTimeFrom && x.ReadingDate <= ShiftTimeFrom ).Select(x => new Data { myDate = x.ReadingDate, myVal = x.ReadingValue }).ToList();</pre><p> 我遇到的问题之一是,客户可能会选择几个月的时间段,他们希望在这段时间的每个周末都看到。 因此,在某些情况下,日期列表可以包含 10 或 20 个日期。</p><p> 我希望使用 Linq 来过滤日期,给我所有日期之间的所有数据List<ShiftTimeFilter>; 以及那些不在这些日期之间的任何东西,我想将它的值设置为零。</p><p> TIA</p></div></object> - Linq query that can use a list of DateTime to filter a List<object> 使用LINQ汇总列表列表 - Use LINQ to aggregate on a list of lists 我可以使用LINQ查询两个列表以填充单个列表吗 - Can I use LINQ to query two Lists to populate a single List 使用LINQ查询列表 - use LINQ to query a list 使用LINQ将列表转换为列表列表 - Use LINQ to Convert a List to a List of Lists 另一个LINQ查询中的LINQ筛选器列表 - LINQ Filter List inside another LINQ Query LINQ 查询返回列表的唯一列表 - LINQ Query Return unique list of lists Linq查询以从列表列表中获取对象 - Linq query to get an object from list of lists 使用LINQ组合列表列表中的属性? - Use LINQ to combine a property in a list of lists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM