Home > OS >  CS1660 Cannot convert lambda expression to type 'Category' because it is not a delegate ty
CS1660 Cannot convert lambda expression to type 'Category' because it is not a delegate ty

Time:09-07

Trying to generate items for a database using Bogus using this code:

var products = new Faker<Product>()
            .StrictMode(true)
            .RuleFor(e => e.Id, f => 0)
            .RuleFor(e => e.Name, (f, u) => f.Commerce.Product())
            .RuleFor(e => e.Price, (f, u) => Convert.ToDecimal(f.Commerce.Price()))
            .RuleFor(e => e.EanCode, (f, u) => f.Commerce.Ean8())
            .RuleFor(e => e.Category, (f, u) => f.Commerce.Categories(5));

And the Categories needed a int parameter but when I add it I get the error Cannot convert lambda expression to type 'Category' because it is not a delegate type.

CodePudding user response:

With this line:

.RuleFor(e => e.Category, (f, u) => f.Commerce.Categories(5))

You are returning a string[] with 5 values, and trying to assign it to a string property. Instead, you could do something like this which gets a n array or length 1, and then selects the first value from it:

.RuleFor(e => e.Category, (f, u) => f.Commerce.Categories(1)[0]);

CodePudding user response:

Can you try adding

using System.Linq; 

from this system class.

Please also add:

using System.Data.Entity;
  • Related