LINQ联合与常数值 [英] LINQ Union with Constant Values

查看:84
本文介绍了LINQ联合与常数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常原始的问题,但我被困住了(我想是新手).我有一个应该向我发送公司列表的函数:另外,我希望调用者也可以为下拉列表指定一个顶部元素(例如无").我有以下代码,如何将顶部元素与返回的SelectList附加在一起?

Very primitive question but I am stuck (I guess being newbie). I have a function which is supposed to send me the list of companies : ALSO, I want the caller to be able to specify a top element for the drop-down list as well.. (say for "None"). I have following piece of code, how I will append the Top Element with the returning SelectList?

    public static SelectList GetCompanies( bool onlyApproved, FCCIEntityDataContext entityDataContext, SelectListItem TopElement )
    {
        var cs = from c in entityDataContext.Corporates
                 where ( c.Approved == onlyApproved || onlyApproved == false )
                 select new
                 {
                     c.Id,
                     c.Company
                 };

        return new SelectList( cs.AsEnumerable(), "Id", "Comapny" );
    }

谢谢!

推荐答案

这应该对您有用:

List<Corporate> corporates =
            (from c in entityDataContext.Corporates
            where (c.Approved == onlyApproved || onlyApproved == false)
            select c).ToList();
corporates.Add(new Corporate { Id = -1, Company = "None" });

return new SelectList(corporates.AsEnumerable(), "Id", "Comapny");

这篇关于LINQ联合与常数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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