user1943020
user1943020

Reputation:

LINQ - type arguments cannot be inferred from the usage in a select

I have the following where objectiveData is: IEnumerable<Objective>

    public IList<Objective> createObjectives()
    {
        var objectiveData = GetContent.GetType5(); 
        var objectives = objectiveData.Select(o => {
            var result = new Objective {
                            Name = o.Name,
                            Text = o.Text
            };
            if (o.Name != null && o.Name.EndsWith("01"))
            {
                result.ObjectiveDetails.Add
                (
                    new ObjectiveDetail
                    {
                        Text = o.Text
                    }
                );
            }
        });
        return objectives.ToList();
    }

I am getting an error on the line with the "select" saying:

The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>
(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,TResult>)' 
cannot be inferred from the usage. Try specifying the type arguments explicitly.

Here's my Objective class:

public partial class Objective : AuditableTable
{
    public Objective()
    {
        this.ObjectiveDetails = new List<ObjectiveDetail>();
    }
    public int ObjectiveId { get; set; }
    public string Name { get; set; }
    public string Text { get; set; }
    public virtual ICollection<ObjectiveDetail> ObjectiveDetails { get; set; }
}

Upvotes: 10

Views: 33522

Answers (3)

It&#39;sNotALie.
It&#39;sNotALie.

Reputation: 22814

First of all, LINQ and side effects are... well, bad. Due to lazy loading, and many other issues. However, what you need is to add a return result; line at then end of your code like so:

var objectives = objectiveData.Select(o => {
        var result = new Objective {
                        Name = o.Name,
                        Text = o.Text
        };
        if (o.Name != null && o.Name.EndsWith("01"))
        {
            result.ObjectiveDetails.Add
            (
                new ObjectiveDetail
                {
                    Text = o.Text
                }
            );
        }
        return result;
    });

However, for this to behave in a more regular manner, I would do it like this:

var objectives = 
    objectiveData.Select(o => new Objective { Name = o.Name, Text = o.Text})
result.ObjectiveDetails.AddRange(
    objectiveData.Where(o => (o.Name ?? "").EndsWith("01"))
                 .Select(o => new ObjectiveDetail { Text = o.Text }));

Upvotes: 0

Plymouth223
Plymouth223

Reputation: 1925

You need

return result;

at the end of your expression.

Upvotes: 20

King King
King King

Reputation: 63387

var objectives = objectiveData.Select(o => {
        var result = new Objective {
                        Name = o.Name,
                        Text = o.Text
        };
        if (o.Name != null && o.Name.EndsWith("01"))
        {
            result.ObjectiveDetails.Add
            (
                new ObjectiveDetail
                {
                    Text = o.Text
                }
            );
        }
        //you miss this
        return result;
    });

Upvotes: 3

Related Questions