0

I got a line of code like so:

ProjectClass pricing = db.Data.Where(model => model.collection == “aaa”);

but I get this error:

Cannot implicitly convert type 'System.Linq.IQueryable<ProjectName.Models.ProjectClass>' to 'ProjectName.Models.ProjectClass'. An explicit conversion exists (are you missing a cast?)

What Am I doing wrong?

Here is my ProjectClass

public class ProjectClass
    {
        [Key]
        public int id { get; set; }
        [DisplayName("Design")]
        public string name { get; set; }
        [DisplayName("Price")]
        [DisplayFormat(DataFormatString = "{0:n0}")]
        public int basePrice { get; set; }
        [DisplayName("Size")]
        public int sqft { get; set; }
        [DisplayName("Collection")]
        public string collection { get; set; }
        [DisplayName("Hidden")]
        public bool hidden { get; set; }
    }
user979331
  • 11,039
  • 73
  • 223
  • 418

4 Answers4

2

Querys for results with any quantity of results use Where

 List<ProjectClass> result = db.Data.Where(model => model.collection == “aaa”).ToList(); 

Querys for only 0 or 1 result use SingleOrDefault

 var result = db.data.SingleOrDefault(model => model.collection == "aaa");

The result will be null if no record matched.

For querying the first matching record use FirstOrDefault

var result = db.data.FirstOrDefault(model => model.collection == "aaa");

The result will be null if no record matched.

Dominik
  • 1,623
  • 11
  • 27
1

Select only one item:

ProjectClass pricing = db.Data.Where(model => model.collection == “aaa”).FirstOrDefault();
Andy T
  • 10,223
  • 5
  • 53
  • 95
  • would that give me all the results where collection would = aaa or just one row? – user979331 Sep 23 '16 at 18:00
  • It will give you first item which match condition. If any element doesn't match condition result will be null. See about FirstOrDefault() https://msdn.microsoft.com/en-us/library/bb340482(v=vs.110).aspx Adn you can see differences from First() http://stackoverflow.com/questions/1024559/when-to-use-first-and-when-to-use-firstordefault-with-linq – kat1330 Sep 23 '16 at 19:29
1
ProjectClass pricing = db.Data.FirstOrDefault(model => model.collection == "aaa");

Currently you are selecting a list. The above will get the first item. If you are expecting exactly one you can do SingleOrDefault instead.

If you want a collection (list) then materialize the result using ToList or ToArray (there are more but these are the common ones).

List<ProjectClass> pricing = db.Data.Where(model => model.collection == "aaa").ToList();
Igor
  • 60,821
  • 10
  • 100
  • 175
0
ProjectClass pricing = db.Data.Where(model => model.collection == “aaa”);

Above code is wrong because Where() will return filtered sequence based on predicate. So, result is MANY not SINGLE element! Even if your condition inside Where() is applicable to SINGLE element result will NEVER be single element.

Exception is thrown because pricing variable is declared as single ProjectClass.

If you are not sure which type you expecting as result you can use:

var pricing = db.Data.Where(model => model.collection == “aaa”);
kat1330
  • 5,134
  • 7
  • 38
  • 61