Linq查询包含对象列表的对象列表 [英] Linq query a List of objects containing a list of object

查看:88
本文介绍了Linq查询包含对象列表的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为绉的foo列表.我想返回foo,其中bar.doritos == "coolRanch"

I have a list of foo called crepes. I want to return foo where bar.doritos == "coolRanch"

class foo
{
    List<bar> item;
    string candy;
    string beer;
}

class bar
{
    string doritos;
    string usb;
}

var item = crepes.item.Where(x => x.doritos == "coolRanch").FirstOrDefault();

从其他线程,我整理了上面的linq查询,但是crepes.item引发错误. 列表不包含'item'的定义,也没有包含第一个参数的'item'的定义...

From other threads, i've pieced together the above linq query, but crepes.item throws an error. "List does not contain a definition for 'item' and no definition for 'item' accepting first argument...

推荐答案

鉴于可丽饼是List<Foo>,您需要在linq查询中添加一个附加级别.

Given that crepes is a List<Foo>, you need to add an additional level to the linq query.

var item = crepes.Where(a => a.item.Any(x => x.doritos == "coolRanch")).FirstOrDefault();

这篇关于Linq查询包含对象列表的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆