0

I need to add data to view model using LINQ My view model is :

public class SearchScrapViewModel 
{ 
public WClass wClass{get; set;} 
public SClass sClass{get; set;} 
public YClass yClass { get; set; } 
} 

public class WClass 
{ 
public string title { get; set; } 
public string link { get; set; } 
} 
public class SClass 
{ 
public string title { get; set; } 
public string link { get; set; } 
} 
public class YClass 
{ 
public string title { get; set; } 
public string link { get; set; } 
}

and i need to use these 3 classes with 3 different LINQ query and then pass data to return View(SearchScrapViewModel);

var wikians = //LINQ Logic 
select new SearchScrapViewModel 
{ 
wClass.link = link.Attributes["href"].Value, //Error: I am not able to add to wClass 
wClass.title = link.InnerText 
}; 

and similarly to other classes

and then pass to return View(SearchScrapViewModel); so that i can access all the 3 classes in View of this controller

How to do that?

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
Man8Blue
  • 1,187
  • 6
  • 21
  • 34

3 Answers3

1

You forgot to create an instance of your WClass:

select new SearchScrapViewModel {
    wClass = new WClass { 
        link = link.Attributes["href"].Value,
        title = link.InnerText 
    }
}; 

Alternatively, you could make WClass (and SClass and YClass) a struct instead of a class, then you don't need to instantiate it. In that case, however, you should probably make the struct immutable.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • ok...i used this...but now how to pass... this to view... i mean using "return View(SearchScrapViewModel )" is not working...Do i need to do some change in viewmodel so that i can pass all class data to View using this main SearchScrapViewModel ? – Man8Blue Jun 06 '12 at 06:36
0

LINQ is not the be-all-end-all, and I dont know that this is the best approach for what you are looking for. I would suggest looking at the Builder Pattern, to accomplish this. If you really want to, you could do this in one LINQ query (using object initializers), but it might not read as clean as a builder would (but that is my two cents):

select new SearchScrapViewModel
{
    wClass = new wClass{title = xyz, link = xyz},
    sClass = new sClass...
    yClass = new yClass...
}
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • I used what @Heinzi suggest. but how to pass this to view... i mean using "return View(SearchScrapViewModel )" is not working...Do i need to do some change in viewmodel so that i can pass all class data to View using this main SearchScrapViewModel ? – Man8Blue Jun 06 '12 at 07:02
0

It is not clear to me why you need a select statement in your example. In any case you can't return SearchScrapViewModel as your return because that is a type and not the instance. Unless your code is simplified for this post and you do need linq, I would suggest:

var wikians = 
new SearchScrapViewModel {
    wClass = new WClass { 
        link = link.Attributes["href"].Value,
        title = link.InnerText 
    }
}; 

return View(wikians);
stephen.vakil
  • 3,492
  • 1
  • 18
  • 23