Vijunav Vastivch
Vijunav Vastivch

Reputation: 4191

Set the first Record Value of LINQ

I have a DBSet which is db.Company and it contains 3 records like

id name is_active

1 company1 1
2 company2 1
3 company3 1

My Created class to transfer the records:

public class CompanyFinal
    { 
        public int id { get; set; }
        public string name { get; set; }
        public string selected { get; set; }
    }

My LINQ looks like this:

(from h in db.Company where h.is_active == 1 select new CompanyFinal { id = h.id, name = h.name, selected = "false" }).ToList();

No doubt this LINQ is working but i need to make the first record to be selected="true" which is the company 1 while doing it in LINQ.

How could i do that? Is it possible?

Thanks in advance

Upvotes: 0

Views: 718

Answers (3)

Daniel Tran
Daniel Tran

Reputation: 6171

From your result:

var companies = (from h in db.Company 
                   where h.is_active == 1 select h).ToList();

var companyFinals = (from h in companies 
                      select new CompanyFinal { 
                         id = h.id, 
                         name = h.name, 
                         selected = "false" 
                     }).ToList();

var firstCompany = companies.FirstOrDefault();

if (firstCompany != null) {
     firstCompany.selected = true;
}

// save result back to database

There is no way to do it in 1 operation other than writing a stored procedure.

Upvotes: 1

Harald Coppoolse
Harald Coppoolse

Reputation: 30454

Since you're making a list, I think that computational the fasted method, which is also the best maintainable (understood by others) would be to just assign the first element:

(Using method syntax, showing an alternative select)

var resultingList = db.Company
    .Where(company => company.is_active == 1)
    .Select(company => new CompanyFinal()
    {
        id = company.id,
        name = company.name,
        selected = "false",
    })
    .ToList();
if (resultingList.Any())
   resultingList[0].selected = true;

If you are not sure whether you want to convert it to a list, for instance because you want to concatenate other Linq functions after it, consider using this overload of Enumerable.Select

var result = db.Company
    .Where(company => company.is_active == 1)
    .Select( (company, index) => new CompanyFinal()
    {
        id = company.id,
        name = company.name,
        selected = (index == 0) ? "true" : "false",
    });

By the way, consider changing your CompanyFinal class such that selected is a bool instead of a string, that would make your code less error prone. For instance, after construction Selected is neither "true" nor "false". Users are able to give it a value like "TRUE" or String.Empty

If your class has users that really need a string property selected instead of a Boolean property consider keeping a get property for it:

public class CompanyFinal
{ 
    public int id { get; set; }
    public string name { get; set; }
    public bool Selected {get; set;}

    // for old user compatability:
    public string selected
    {
        get { return this.Selected ? "true" : "false"; }
    }
}

The following will go wrong:

if (companyFinals[0].selected == "True") ...

versus:

 if (companyFinals[0].Selected)

Upvotes: 2

Devesh
Devesh

Reputation: 4550

What if we define a local variable

               var counter = 1;
               (from h in db.Company where h.is_active == 1 select new CompanyFinal { id = h.id, name = h.name, selected =  (counter++ == 1 ? "true" :"false") }).ToList();

Upvotes: 2

Related Questions