GANI
GANI

Reputation: 2049

converting into string in linq to entites

Here DisplayName is of type string, foo is object. When I use foo.ToString(), it throws an error.

I have the code:

var completionTimeModels =
    from timeline in context.processTimelines
    join asst in context.timeKeeping on timeline.Id equals asst.Id into TT
    from TimeModels in TT.DefaultIfEmpty()

    let foo = TimeModels.ExternalId !=null ? TimeModels.Name: timeline.circuitName
    select new Items()
    {
        Name = timeline.Name
        DisplayName = foo
    };

and when I do the following:

DisplayName = TimeModels.ExternalId !=null ? TimeModels.Name.ToString(): timeline.circuitName.ToString(),

it gives an error:

Linq cannot handle .ToString()

Upvotes: 1

Views: 192

Answers (1)

BrokenGlass
BrokenGlass

Reputation: 160862

ToString() is not defined for Linq to Entities - you can project to an anonymous class, then switch to Linq to Objects for the final projection:

    (...
    select new 
    { 
        Name = timeline.Name, 
        DisplayName = TimeModels.ExternalId !=null ? TimeModels.Name : timeline.circuitName
    })
    .AsEnumerable()
    .Select(x=> new Items()
     {
              Name = x.Name
              DisplayName = x.DisplayName.ToString()
     };

Why do you have to do this in the first place though? What type are TimeModels.Name and timeline.circuitName ?

Upvotes: 2

Related Questions