Linq查询未返回所需的结果集 [英] Linq query not returning required result set

查看:92
本文介绍了Linq查询未返回所需的结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想使用linq从列表中选择特定项目,以下是我的示例代码,在这里我试图获取IsC标志为"Y"的项目,但我没有得到.我应该只在结果集中得到两个项目,但是要得到所有四个项目.

我在这里使用两种方法,但两种方法均未返回所需的结果.

Hi,

I want to select particular items from a list using linq, below is my sample code, here I am trying to get the items in which IsC flag is "Y", but I am not getting that. I should get only two items in result set, but am getting all four items.

I am using two ways here, but both are not returning the required results.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            List<MainClass> mnc = new List<MainClass>();
            MainClass mn = new MainClass();
            Item I1 = new Item();
            I1.IsC = "Y";
            Item I2 = new Item();
            I2.IsC = "N";
            Item I3 = new Item();
            I3.IsC = "Y";
            Item I4 = new Item();
            I4.IsC = "N";
            mnc.Add(mn);
            mn.Items.Add(I1);
            mn.Items.Add(I2);
            mn.Items.Add(I3);
            mn.Items.Add(I4);

            var data1 = from mncItems in mnc
                              from item in mncItems.Items
                              where (item.IsC == "Y")
                              select mncItems;

            var data = from mncItems in mnc
                             where mncItems.Items.Exists(delegate(Item item) { return item.IsC == "Y"; })
                             select mncItems;

            MainClass mn1 = new MainClass();
            mn1 = data1.ToList()[0];

            MainClass mn2 = new MainClass();
            mn2 = data.ToList()[0];

        }
    }
    public class MainClass {
        private List<Item> items;
        public List<Item> Items
        {
            get { return items; }
            set { items = value; }
        }
        public MainClass()
        {
            this.Items = new List<Item>();
        }
        private int id;
        public int Id {
            get {
                return id;
            }
            set {
                id = value;
            }
        }
    }
    public class Item {
        private string isc;
        public string IsC
        {
            get
            {
                return isc;
            }
            set
            {
                isc = value;
            }
        }
    }



有人可以帮我吗?
谢谢



Can someone please help me in this?
Thanks

推荐答案

尝试类似的方法:

Try something like this:

List<MainClass> mainClassItems = new List<MainClass>();
MainClass mainClass = new MainClass();
mainClass.Items.Add(new Item() { IsC = "Y" });
mainClass.Items.Add(new Item() { IsC = "N" });
mainClass.Items.Add(new Item() { IsC = "Y" });
mainClass.Items.Add(new Item() { IsC = "N" });
mainClassItems.Add(mainClass);
var items = from item in mainClassItems[0].Items where item.IsC == "Y" select item;



希望这对您有帮助



Hope this helps


var data1 = from i in mnc.First()
            where i.IsC == "y"
            select i




制造
Doriath21




Mfg
Doriath21


我假设您正在尝试查找具有IsC =="Y"的所有项目,无论它们位于哪个主类中.工作:

I assume you''re trying to find all of the Items that have IsC == "Y" regardless of what main class they''re in. This is how I got it to work:

MainClass foundItems = new MainClass();
foreach (MainClass mc in mnc)
{
    var data1 = (from item in mc.Items
                 where item.IsC == "Y"
                 select item);
    foundItems.Items.AddRange(data1.ToArray());
}



您的原始代码中没有任何工具可以识别该物品来自何处的MainClass对象.



There''s no facility in your original code to identify WHICH MainClass object the Item came from.


这篇关于Linq查询未返回所需的结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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