Anup
Anup

Reputation: 9746

Implicitly typed local variables must be initialized

Here is the Code :- How to initialize the data?

var data;

if (mode == "view")
                {
                    data = (from a in ctx.tblEmployee
                            where a.CompanyId == companyId
                            join b in ctx.tblTO_ShiftSchedule on a.Id equals b.EmployeeId
                            where b.CompanyId == companyId                            
                            select new { a, b, c, d }).ToList();
                }
                else
                {
                    data = (from a in ctx.tblEmployee
                            where a.CompanyId == companyId                            
                            select new { a, b, c, d }).ToList();
                }

Upvotes: 1

Views: 5239

Answers (3)

nitin madan
nitin madan

Reputation: 31

You cannot Initialize an Anonymous type If your are aware of the structure you would get from your linq query i would suggest you to create a Named Structure For example

class Data
{
    Public Property A {get; set;}
    Public Property B {get; set;}
    Public Property C {get; set;}
    Public Property D {get; set;}
}

and modify your existing code to look like this

var data = List<Data>;

if (mode == "view")
{
    data = (from a in ctx.tblEmployee
           where a.CompanyId == companyId
           join b in ctx.tblTO_ShiftSchedule on a.Id equals b.EmployeeId
           where b.CompanyId == companyId                            
           select new Data with { A=a, B=b, C=c, D=d }).ToList();
}
else
{
    data = (from a in ctx.tblEmployee
            where a.CompanyId == companyId                            
            select new Data with { A=a, B=b, C=c, D=d }).ToList();
}

Upvotes: 0

Michael Spranger
Michael Spranger

Reputation: 495

The variables c and d (and b in the second part) in your query are not defined, so I am somewhat guessing at what you want do achieve. Hence my code is not complete.

You need a helper method:

IEnumerable<T> GetDefaultEnumerable<T>(T instance)
{
    return Enumerable.Empty<T>();
}

Then initialize data with a dummy object of the anonymous type

var data = GetDefaultEnumerable(new { a = new tblEmployee(), b = new tblTO_ShiftSchedule(), c = ..., d = ... });

However, in most cases it should be clearer with using a normal type. If possible, you might also consider refactoring the condition into the query.

Upvotes: 0

burning_LEGION
burning_LEGION

Reputation: 13460

you can't initialize anonymous type, so:

var data = mode == "view" 
? (from a in ctx.tblEmployee
                        where a.CompanyId == companyId
                        join b in ctx.tblTO_ShiftSchedule on a.Id equals b.EmployeeId
                        where b.CompanyId == companyId                            
                        select new { a, b, c, d }).ToList()
: (from a in ctx.tblEmployee
                        where a.CompanyId == companyId                            
                        select new { a, b, c, d }).ToList();

Upvotes: 9

Related Questions