1

I am trying to combine two fields for e.g. FullName = FirstName + LastName

How can I accomplish in the following code?

IEnumerable<Item> Items = from item in _entities.Items
where Item.ID == Id 
orderby contractItem.Item.ItemName
select new Item
{
   Id = item.ID,
   ...
   CreatedBy = (from employee in _entities.Employees
                where employee.UserID == item.CreatedBy
                select new {
                             FirstLast = employee.FName + " " + employee.LName}).FirstOrDefault(), // <---- gives me an error
   ...
};
1
  • what error does it give you? Commented Feb 12, 2014 at 17:08

1 Answer 1

4

Looks like CreatedBy in Item is a type of string property and since in your query you are using select new it is creating an anonymous type. You can modify it like:

    (from employee in _entities.Employees
    where employee.UserID == contractItem.CreatedBy
    select employee.FName + " " + employee.LName).FirstOrDefault()

So your complete query would be like:

IEnumerable<Item> Items = from item in _entities.Items
where Item.ID == Id 
orderby contractItem.Item.ItemName
select new Item
{
   Id = item.ID,
   ...
   CreatedBy = (from employee in _entities.Employees
                where employee.UserID == item.CreatedBy
                select employee.FName + " " + employee.LName).FirstOrDefault(), 
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.