user1981120
user1981120

Reputation: 127

SQL TO Linq, How to return an object and fill properties

i have a question, i want to create a linq query that returns a list of object.

This is the model

public class Test
{
[Key]
public int ID { get; set; }

[Required]
[StringLength(5)]
public string Code { get; set; }

[Required]
[StringLength(50)]
public string Name { get; set; }

[NotMapped]
public string Reference { get; set; }
}

The query that i want to do is simple: context.Test.ToList(); this returns the database mapping Reference is null since is not part of the table.

Now if i create a linq query i know that i can do select new { all fields here } i want to avoid this:

select new Test
{
Reference = r,
ID = t.ID,
Code = t.Code,
Name = t.Name
}).ToList();

is it possible to do something like this

(from t in context.Test
join r in context.Reference on f.ID equals r.ID
select new
{
  t.Reference = r.Reference,
  t
}).ToList();

i want to set the Reference value inside the same query, is that possible?

Upvotes: 2

Views: 2107

Answers (4)

Callback Kid
Callback Kid

Reputation: 718

Don't select an anonymous object, just create a new T from the one you have.

(from t in context.Test
join r in context.Reference on t.ID equals r.ID
select new Test
{
  Reference = r,
  ID = t.ID,
  Code = t.Code,
  Name = t.Name
}).ToList();

EDIT:

To avoid having to manually copy over all the properties

public class Test
{
    public int ID { get; set; }

    public string Code { get; set; }

    public string Name { get; set; }

    public string Reference { get; set; }

    public Test CopyWithReference(string reference)
    {
        var copy = (Test)this.MemberwiseClone();
        copy.Reference = reference;
        return copy;
    }
}

Then...

(from t in context.Test
join r in context.Reference on t.ID equals r.ID
select t.CopyWithReference(r)).ToList();

Upvotes: 2

Hilarioc
Hilarioc

Reputation: 13

Try:

var result = context.Test.Include("Reference").ToList();

or:

var result = context.Test.Include(t => t.Reference).ToList();

or Try Lambda Expressions:

var result = context.Test.Select(t => new {
                                t,
                                t.Reference = t.Reference.Select(r => new { 
                                              r.Reference })
             }).AsEnumerable().Select(x => x.r).ToList();

Upvotes: 0

Ivan Stoev
Ivan Stoev

Reputation: 205579

What are you asking is not directly supported in LINQ to Entities - neither projection to entity type, nor expression block which is the only way to assign properties of an existing object.

As usual, the typical workaround is to split the query on two parts - one being LINQ to Entities query selecting the necessary data (usually into intermediate anonymous type), then switch to LINQ to Objects with AsEnumerable() and do the rest - in this case using block inside Select:

var result = 
    (from t in context.Test
     join r in context.Reference on f.ID equals r.ID
     select new { t, r.Reference }
    ).AsEnumerable()
    .Select(x =>
    {
        x.t.Reference = x.Reference;
        return x.t;
    }).ToList();

Upvotes: 4

jdweng
jdweng

Reputation: 34421

Try following :

(from t in context.Test
join r in context.Reference on f.ID equals r.ID
select new Test() 
{
  ID = t.ID,
  Code = t.Code,
  Name = t.Name,
  Reference = r.Reference
}).ToList();

Upvotes: 0

Related Questions