This is basically a mess. You have copied an answer from another question, without understanding what it does, changed it in a way that breaks the very thing that code was supposed to fix, and left the part of that code which was already broken (which the OP of the other question commented on).
The original thing you thought you wanted to do is this:
Original non-working code
var result = (from cs in context.CollegeDetails
select new CollegeDetail
{
CollegeName = cs.CollegeName,
// ...
CreatedBy = cs.CreatedBy
});
As you found out, and so did the OP of The entity cannot be constructed in a LINQ to Entities query you can't do this. You can't create entities like this, for reasons which are explained on that question - basically if you create an entity like this, it wouldn't be clear what the representation in the db was.
Unfortunately, rather than find that question, which has a pretty decent answer with a discussion of why it works that way, you found The entity or complex type ' ' cannot be constructed in a LINQ to Entities query The (original) suggestion in the answer you copied from that question was
Original broken answer to slightly different question
var result = (from cs in context.CollegeDetails
select new
{
CollegeName = cs.CollegeName,
// ...
CreatedBy = cs.CreatedBy
}
)
.Select(c => new CollegeDetail
{
c.CollegeName,
// ...
c.CreatedBy
}
);
The problem with this one is that they didn't use the names in the second constructor. They could have left out the names in the first constructor, since it's an anonymous type, but didn't. When they left them out in the second type it was interpreted as the other use of braces after a constructor, a collection initializer. Since CollegeDetail
is not a collection, it produced the error message you saw.
Now, the OP of that question pointed out that the answer didn't work (although 11 people voted it up). The answerer misunderstood the error message and added a completely unnecessary ToList()
to the end of the code. This wouldn't have fixed anything. Their final suggestion was:
Revised, still broken answer to slightly different question
var result = (from cs in context.CollegeDetails
select new
{
CollegeName = cs.CollegeName,
// ...
CreatedBy = cs.CreatedBy
}
)
.Select(c => new CollegeDetail
{
c.CollegeName,
// ...
c.CreatedBy
}
).ToList();
You took this broken code, and for reasons known only to yourself, changed the anonymous type in the first select
back to CollegeDetail
. (Perhaps you aren't familiar with anonymous types and thought it was a typo, or perhaps it was your typo.) This re-introduced the original problem, made the second select
redundant, and didn't fix the broken initializer.
Incorrect, still broken copy of revised answer to slightly different question
var result = (from cs in context.CollegeDetails
select new CollegeDetail
{
CollegeName = cs.CollegeName,
// ...
CreatedBy = cs.CreatedBy
}
)
.Select(c => new CollegeDetail
{
c.CollegeName,
// ...
c.CreatedBy
}
).ToList();
You also have the unnecessary ToList
- note that in your case you then call ToList
again on the result, immediately afterward. (So you have a redundant select and a redundant ToList()
. This happens a lot in cargo cult coding.)
Now, the two other answerers to this question, saw that your second select was pointless and led to an error because of the broken initializer, and removed it. They didn't know what problem you used the double select
to try and solve, and so they just removed it, rather than help you with that problem. All this did was take you back to your original problem, the code which doesn't work because it maps an entity onto another entity. In other words, they fixed the completely spurious error that was introduced from the code you copied.
Partial fix, still broken, of incorrect copy of revised answer to slightly different question aka original non-working code
var result = (from cs in context.CollegeDetails
select new CollegeDetail
{
CollegeName = cs.CollegeName,
// ...
CreatedBy = cs.CreatedBy
});
This is now a round-trip - all the answers to this question have so far achieved is to get you back to your original problem.
The solution for this is not to blindly copy code without understanding how it fixes the problem. Don't trust any answerer that posts any code without explaining how it solves the problem.
If you 'just want the codez'
Put the field names back into the second select
:
Fixed code
var result = (from cs in context.CollegeDetails
select new
{
CollegeName = cs.CollegeName,
// ...
CreatedBy = cs.CreatedBy
}
)
.Select(c => new CollegeDetail
{
CollegeName = c.CollegeName,
// ...
CreatedBy = c.CreatedBy
}
);
You are free to take them out of the first one though:
Marginally neater fixed code
var result = (from cs in context.CollegeDetails
select new
{
cs.CollegeName,
// ...
cs.CreatedBy
}
)
.Select(c => new CollegeDetail
{
CollegeName = c.CollegeName,
// ...
CreatedBy = c.CreatedBy
}
);
Don't repeat ToList()
twice, unless you really want to. And think about doing what @Rawling suggested:
What you probably wanted
return context.CollegeDetails.ToList()
You don't seem to be transforming the data, and it's not clear that you want to write anything back to the database. Unless you know this isn't what you want, it probably is.
If you don't just want the codez
Make sure you know what an anonymous type is and how to use one. Make sure you know exactly what a lambda means. Find out what the difference between an IEnumerable
and a List
is, and what ToList
does. Learn about the Entity Framework, why an entity is different from an object, and how Linq to Entities (and Linq to SQL) work differently from Linq to Objects. (Jon Skeet's blog is the go-to for learning about Linq: http://msmvps.com/blogs/jon_skeet/Default.aspx) Read this answer and the discussion in comments: https://stackoverflow.com/a/5325861/1737957 Read my answer again, you will only be able to fix your own code when you understand how you got here. Good luck!