C# Lambda 多个条件,子列表

发布于 2024-10-17 06:21:34 字数 353 浏览 1 评论 0原文

想知道以下内容是否可行,但我仍在了解 lambda 表达式。

    GetAll(x => x.Username.ToUpper().Contains(SEARCH) 
             && x.AddressList.Type_ID == 98.ToList();

这段代码的问题在于“Address”是一个列表。本质上,我们希望在 1) 对 UserName 执行搜索和 2) 匹配子列表的属性后返回 List。

从语义上讲,上面的代码不起作用,因为“Type_ID”不可用作选择,因为 AddressList 是一个列表,而不是单个实体。

这能实现吗?

Would like to know if the following is possible, still getting my head around lambda expressions.

    GetAll(x => x.Username.ToUpper().Contains(SEARCH) 
             && x.AddressList.Type_ID == 98.ToList();

The trouble with this code is that "Address" is a List. Essentially we would like to return List after 1) performing a search against UserName and 2) matching a property of a child list.

Semantically the code above does not work as 'Type_ID' is not available as a selection due to the fact that AddressList is a List, not a single entity.

Can this be accomplished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生一梦 2024-10-24 06:21:34

你的意思是类似的东西吗?

GetAll(x => x.Username.ToUpper().Contains(SEARCH) 
             && x.AddressList.Any(e => e.Type_ID == 98));

Do you mean something like?

GetAll(x => x.Username.ToUpper().Contains(SEARCH) 
             && x.AddressList.Any(e => e.Type_ID == 98));
瞄了个咪的 2024-10-24 06:21:34

有一些对列表进行操作的扩展方法,但仍然不清楚您想要做什么。您想要的结果是 AddressList 中的任意项的 Type_ID 为 98 吗?如果是这样,请使用 Any 函数。如果列表中的所有地址都必须具有该类型 ID,则使用“全部”。这是一些示例代码:

class Address
{
   public int Type_ID { get; set; }
   public Address(int Type_ID)
   {
      this.Type_ID = Type_ID;
   }
}

class Entry
{
   public List<Address> AddressList { get; set; }
   public string UserName { get; set; }
   public Entry(string name, List<Address> addresses)
   {
      AddressList = addresses;
      this.UserName = name;
   }
}

class EntryList
{
   private List<Entry> entries;

   public IEnumerable<Entry> GetAll(Func<Entry, bool> filter)
   {
      return entries.Where(filter).ToArray();
   }

   public EntryList(List<Entry> entries)
   {
      this.entries = entries;
   }
}

static void Main(string[] args)
{

   EntryList e1 = new EntryList(new List<Entry>()
      {new Entry("Fred with 1 98 address", new List<Address> { new Address(97), new Address(98) }),
         new Entry("Bob with no 98 address", new List<Address> { new Address(96), new Address(97) }),
         new Entry("Jerry with all 98 addresses", new List<Address> { new Address(98), new Address(98) })});

   var all98 = e1.GetAll(x => x.AddressList.All(y => y.Type_ID == 98));
   var any98 = e1.GetAll(x => x.AddressList.Any(y => y.Type_ID == 98));

   Console.WriteLine("Results for ALL:");
   foreach (var e in all98)
      Console.WriteLine(e.UserName);
   Console.WriteLine("Results for ANY:");
   foreach (var e in any98)
      Console.WriteLine(e.UserName);
}

输出是:

Results for ALL:
Jerry with all 98 addresses
Results for ANY:
Fred with 1 98 address
Jerry with all 98 addresses

There are extension methods that operate on lists, but it's still not clear what you want to do. Do you want results where any item in AddressList has a Type_ID of 98? If so, use the Any function. If all addresses in the list must have that type ID than use All. Here's some sample code:

class Address
{
   public int Type_ID { get; set; }
   public Address(int Type_ID)
   {
      this.Type_ID = Type_ID;
   }
}

class Entry
{
   public List<Address> AddressList { get; set; }
   public string UserName { get; set; }
   public Entry(string name, List<Address> addresses)
   {
      AddressList = addresses;
      this.UserName = name;
   }
}

class EntryList
{
   private List<Entry> entries;

   public IEnumerable<Entry> GetAll(Func<Entry, bool> filter)
   {
      return entries.Where(filter).ToArray();
   }

   public EntryList(List<Entry> entries)
   {
      this.entries = entries;
   }
}

static void Main(string[] args)
{

   EntryList e1 = new EntryList(new List<Entry>()
      {new Entry("Fred with 1 98 address", new List<Address> { new Address(97), new Address(98) }),
         new Entry("Bob with no 98 address", new List<Address> { new Address(96), new Address(97) }),
         new Entry("Jerry with all 98 addresses", new List<Address> { new Address(98), new Address(98) })});

   var all98 = e1.GetAll(x => x.AddressList.All(y => y.Type_ID == 98));
   var any98 = e1.GetAll(x => x.AddressList.Any(y => y.Type_ID == 98));

   Console.WriteLine("Results for ALL:");
   foreach (var e in all98)
      Console.WriteLine(e.UserName);
   Console.WriteLine("Results for ANY:");
   foreach (var e in any98)
      Console.WriteLine(e.UserName);
}

The output is:

Results for ALL:
Jerry with all 98 addresses
Results for ANY:
Fred with 1 98 address
Jerry with all 98 addresses
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文