add a record to return result in memory

2019-09-09 23:14发布

问题:

I have some 3 different dropdown list and each dropdown fetch data from database table.

Tables

  1. TableSchool
  2. TableSubject
  3. TableClass

As the for load data is fetch using LINQ To SQL. I want to add first row of dropdown to --Select School-- --Select Subject-- --Select Class--

How can that be done?

Code -

 var list = (from i in context.MAT_TableSchool
                    select
                        new
                        {
                            SchoolName= string.Format("SN - {0}", i.SchoolName),
                            SchoolId = i.SchoolId
                        });

this._schoolDropdown.DataSource = list; this._schoolDropdown.DataBind();

What I want is to add a record SchoolName="-Select-" SchoolId=0 at top of this list or dropdown

回答1:

You don't show a lot of code, so I can only be very generic:

var schoolItems = new string[]{ "--Select School--" }
     .Concat(tableSchoolItems);

Where tableSchoolItems is what you read from the database

** Edit **

var list = new [] { new
            {
                SchoolName = "--Select--",
                SchoolId = 0
            } }.Concat(
    from i in context.MAT_TableSchool 
    select new
            {
                SchoolName= string.Format("SN - {0}", i.SchoolName),
                SchoolId = i.SchoolId
            });