如何在LINQ查询中添加其他行?

时间:2011-08-01 09:58:40

标签: c# linq union

我有一个LINQ查询,它从数据集中检索行并将其绑定到Winforms组合框:

namespace KimHongAutoAccessory.Forms
{
    public partial class EngineFilter : Form
    {
        public EngineFilter()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EngineFilter_Load(object sender, EventArgs e)
        {

            this.engineFilterTableAdapter.Fill(this.manufacturerEngine.EngineFilter);

            var manufacturers = (from m in manufacturerEngine.EngineFilter
                                 select new { m.ManufacturerID, m.Manufacturer })
                                 .Distinct().ToList();

            cboManufacturerFilter.DataSource = manufacturers.ToArray();
            cboManufacturerFilter.DisplayMember = "Manufacturer";
            cboManufacturerFilter.ValueMember = "ManufacturerID";


        }
    }
}

但是我想在组合框中添加一个联合项目,比如单词“[All]”:

|-------|
|TOYOTA |
|-------|
|KUBOTA |
|-------|
|FORD   |
|-------|
|[All]  |
|-------|

有人可以给我一招吗?

3 个答案:

答案 0 :(得分:4)

据推测,您的“(全部)”项目的ID会有一个特殊值,因此您知道它何时被选中。我们假设它的ID是-1。

var manufacturers = (from m in manufacturerEngine.EngineFilter
                     select new { m.ManufacturerID, m.Manufacturer }
                    ).Distinct().ToList();

manufactorers.Add(new { ManufacturerID = -1, Manufacturer = "(All)" });

cboManufacturerFilter.DataSource = manufacturers.ToArray();
// ... etc ...

答案 1 :(得分:4)

这是一个在LINQ中使用Union的简单示例,为清晰起见使用数字。

        var A = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        var L = (from a in A where a > 5 select a)
            .Distinct()
            .Union(new int[]{-1})
            .ToList();

您网域中的OR:

        var manufacturers = new []{ManufacturerID=-1, Manufacturer="(ALL)"}).AsEnumerable()
                      .Union(from m in manufacturerEngine.EngineFilter 
                             select new { m.ManufacturerID, m.Manufacturer })
                     .Distinct()
                     .ToList();

工作示例(更接近问题域)

    public class Manufacturer
    {
        public Manufacturer(int id, string name)
        {
            this.id = id;
            this.name = name;
        }

        public int id { get; set; }
        public string name { get; set; }
    }

    [Test]
    public void test222()
    {
        var L = new List<Manufacturer>() { new Manufacturer(1, "Abc"), new Manufacturer(2, "def"), new Manufacturer(3, "hij"), new Manufacturer(4, "klm"), new Manufacturer(5, "nop"), new Manufacturer(6, "qrs"), new Manufacturer(7, "tuv"), new Manufacturer(8, "wxyz") };

        var manufacturers = 
            new Manufacturer[] { new Manufacturer(-1, "[ALL]") }.AsEnumerable()
            .Union(from m in L select new Manufacturer(m.id, m.name)).Distinct().ToList();            
    }

N.B。如果你想在没有构造函数的情况下初始化,请执行以下操作:

    new Manufacturer() {id=-1,name="[All]"}

这意味着同样的事情。

答案 2 :(得分:1)

我知道这已经过时了,但我想我会把这个留给下一个人=) 我能够将匿名对象添加到下面的匿名列表(.net 4.5)

var manufacturers = (from m in manufacturerEngine.EngineFilter
                     select new 
                     { 
                         ManufacturerID = (int)m.ManufacturerID, 
                         Manufacturer = m.Manufacturer 
                     }).ToList();

var allItem = new {
    ManufacturerID = (int)-1,
    Manufacturer = "ALL"
};

manufacturers.add(allItem);

希望这有帮助。