views:

175

answers:

4

Hi;

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" );
 }

Thanks!

A: 
cs.ToList().Insert(0, new { TopElement.ID, TopElement.Company });
Paul Creasey
TopElement is SelectListItem it only contains Value and Text.
Nick Berardi
+1  A: 

This method has always worked for me.

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 SelectListItem {
                                     Value = c.Id,
                                     Text = c.Company
                             };

            var list = cs.ToList();
            list.Insert(0, TopElement);

            var selectList = new SelectList( list, "Value", "Text" );
            selectList.SelectedValue = TopElement.Value;

            return selectList;
    }

Update forgot the lesson I learned when I did this. You have to output the LINQ as SelectListItem.

Nick Berardi
The best overloaded method match for 'System.Collections.Generic.List<AnonymousType#1>.Insert(int, AnonymousType#1)' has some invalid argumentsANDArgument '2': cannot convert from 'AnonymousType#2' to 'AnonymousType#1'.I guess I forgot to specify that entityDataContext.Corporates is LINQ to SQL class... Compiler is not allowing the addition of anonymous type....
effkay
Try this update.
Nick Berardi
Yes; I guess now it will work; thanks for the effort!
effkay
A: 

You could convert it to a list as indicated or you could union the IQueryable result with a constant array of one element (and even sort it):

 static void Main(string[] args)
 {
  var sampleData = new[] {
   new { Id = 1, Company = "Acme", Approved = true },
   new { Id = 2, Company = "Blah", Approved = true }
  };

  bool onlyApproved = true;

  var cs = from c in sampleData
     where (c.Approved == onlyApproved || onlyApproved == false)
     select new
     {
      c.Id,
      c.Company
     };

  cs = cs.Union(new [] {new { Id = -1, Company = "None" }}).OrderBy(c => c.Id);

  foreach (var c in cs)
  {
   Console.WriteLine(String.Format("Id = {0}; Company = {1}", c.Id, c.Company));
  }

  Console.ReadKey();
 }
ongle
+1  A: 

This should work for you:

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");
Mark Byers
very tricky... code got compiled; testing now; post update once confirmed..... thx.
effkay
Worked! Thanks!
effkay
RECOMMENDATION: The last select into variable "cs" is not required - you can pass corporates directly into the constructor of SelectList...
effkay
Thanks. I have now removed the last select.
Mark Byers