Zebra
Zebra

Reputation: 66

How to add a new item to a list pre-filled by linq?

I have a list that pre-filled .Imagine it has 3 item that filled.Now,I want add to list an new item that is null.Befor I use AddRange for a list that is null,but for this case I don't know do, How to do it by linq ?

List<paramodel> Params=_readonlyService.GetAll();

I use AddRange but not recognized for this.

Upvotes: 0

Views: 85

Answers (2)

Zein Makki
Zein Makki

Reputation: 30022

Just check for NULL then:

For Single Item:

List<paramodel> Params=_readonlyService.GetAll();
if(Params!= null)
    Params.Add(toBeAddedObject);

For Multiple Items:

List<paramodel> Params=_readonlyService.GetAll();
    if(Params!= null)
        Params.AddRange(secondList);

Upvotes: 2

Hitesh Thakor
Hitesh Thakor

Reputation: 471

    List<PetViewModel> Params = new List<PetViewModel>();

    Params = _readonlyService.GetAll().ToList();
    if (Params.Count() != 0)
    {
        Params.Union(SecondList);
    }
    else
    {
      Params = SecondList;
    }

Upvotes: 0

Related Questions