OdataController query plan not optimal because of DTO mapping - c#

When mapping a DAL object to DTO object I get an unexpected query.
I have some DAL objects:
public class MainDalObject
{
public Guid Id { get; set;}
public string Description { get; set; }
public ICollection<SubDalObject> Subs { get; set; }
}
public class SubDalObject
{
public Guid Id { get; set;}
public string Description { get; set; }
public MainDalObject Main { get; set; }
}
And my DTO classes:
public class MainObject
{
public Guid Id { get; set;}
public string Name { get; set; }
public IEnumerable<SubObject> Subs { get; set; }
}
public class SubObject
{
public Guid Id { get; set;}
public string Name { get; set; }
public MainDalObject Main { get; set; }
}
My MainObject controller contains a IQueryable method:
public IQueryable<MainObject> Get()
{
return (from m in Context.Get<MainDalObject>()
select new MainObject
{
Id = m.Id,
Name = m.Description,
Subs = m.Subs.Select(s => new SubObject
{
Id = s.Id,
Name = s.Name
}
});
}
This works fine, however the query is not optimal. When i trigger the query on /api/MainObject , I am not selecting the subitems at all. But when i look at the query it is selecting the subItems anyway.
However when I change the query to /api/MainObject?$select=Id,Name , the query is not selecting the SubObjects.
So what I am expecting is that somewhere in the WebApi framework, when no SelectExpandFilter is used, the responsewriter is doing a ToList(), without specifying the Select statement.
I am looking for the best place to fix this problem, I could probably set a select expand ODataQueryOption that could fake an select or expand call, but I am not sure if that is the way to go.

If someone else runs in this problem, this is what I have done to fix it. The select expand query should be derived from the edmmodel in the request context, but this is basically how it works.
public virtual IQueryable<TDTO> Get(ODataQueryOptions queryOptions)
{
if (queryOptions.SelectExpand == null)
{
var selectOption = new SelectExpandQueryOption("Id,Name", string.Empty, queryOptions.Context);
Request.SetSelectExpandClause(selectOption.SelectExpandClause);
}
return (from m in Context.Get<MainDalObject>()
select new MainObject
{
Id = m.Id,
Name = m.Description,
Subs = m.Subs.Select(s => new SubObject
{
Id = s.Id,
Name = s.Name
}
});
}

Related

How to join tables using LINQ to Entities query?

I have a simple db structure:
public class Person
{
[Key]
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Transport
{
[Key]
public int TransportID { get; set; }
public string Model { get; set; }
public string Brand { get; set; }
}
public class Accident
{
[Key]
public int AccsidentID { get; set; }
public DateTime AccidentDate { get; set; }
public int TransportID { get; set; }
[ForeignKey("TransportID")]
public virtual Transport Transport { get; set; }
public int PersonID { get; set; }
[ForeignKey("PersonID")]
public virtual Person Person { get; set; }
}
I need to create a list of accidents, wich I could pass to WPF form (using MVVM)
First I created new class witch I would like to see in my GridControl
public class AccsidentObject
{
[Key]
public int AccidentID { get; set; }
public DateTime AccidentDate { get; set; }
public int TransportID { get; set; }
public string Model { get; set; }
public string Brand { get; set; }
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Could you please give me examples:
if I want to get list of all values from Accident table including data from Transport and Person tables
if I want to get Accident list grouped by TransportID (also include data from Person and Transport tables)
I am getting data from Linq query:
var result = from ac in DBContext.Accidents select ac;
List<Accident> accidentList = result.toList();
But I need to add some fields to list from other tables, what would be a code?
What do I do wrong and could not construct a list of AccidentObject, maybe there are some mistake in my DBContext, lists o something... Could you please help me to understand List elements??
Considering to 2 part I wrote:
var result = from ac in DBContext.Accidents select ac;
result = result.GroupBy(g => g.TransportID).toList();
And now I need to add some Transport details and format AccsidentObject list ...
To get an entity (or collection of entities) with associations eagerly populated use the Include extension method, or include in a final projection into your type:
var res = await (from a in ctx.Accidents
select new AccsidentObject {
AccidentID = a.AccidentID,
TransportID = a.Transport.TransportID,
Model = a.Transport.Model,
// …
}).ToListAsync();
You can use groupby in a LINQ comprehension expression to group by something. In the result the Key property is the thing grouped by and each instance is a collection of all things grouped by.
var res = await (from a in ctx.Accidents
group by a.TransportID into g
select new {
TransportID = g.Key,
Accidents = g
}).ToListAsync();
In the resulting anonymous types the Accidents property with be a collection of Accident.
var accidents = DBContext.Accidents.Select( a => new AccidentObject
{
AccidentID = a.AccidentId,
AccidentDate
TransportID
Model
Brand = a.Transport.Brand,
PersonID = a.Person.PersonID,
FirstName
LastName
});
and fill in the blanks in much the same way.
here's a linq example without using lambda expressions, that includes a group by clause if you prefer it: Linq to sql select into a new class

Linq to Entity : add filter to child entities of child entities

Here is my problem, this is now 1h I'm searching on web, can't find solution.
I have these classes :
public class User
{
public string Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Tipster> Tipsters { get; set; }
}
public class Tipster
{
public string Id { get; set; }
public string Name { get; set; }
public bool Visible { get; set; }
public virtual ICollection<Bet> Bets { get; set; }
}
public class Bet
{
public string Id { get; set; }
public string Name { get; set; }
public string State { get; set; }
public virtual Tipster Tipster { get; set; }
}
So a User have a collection of Tipster that he's following, and tipsters can push bets, so they have collection of bets, and a bet have only one tipster that create it.
I'm working on WEB API, using Entity Framework, and I want using Linq to Entity to do this :
Get User by Id, including the collection of tipsters where visible == true, including bets they created but filtering these, with State = "pending".
I can filter the Tipsters Visible state,
using .Any()
or
using
context.Entry(user).Collection( c => c.Tipsters ).Query(...)
But I don't know how to filter the child entities (bets) of my child entities (tipsters).
Try this. Might be more elegant ways to do it:
var result = (from user in users
select new User
{
Id = user.Id,
Name = user.Name,
Tipsters = user.Tipsters.Where(x => x.Visible).Select(y => new Tipster
{
Id = y.Id,
Name = y.Name,
Visible = y.Visible,
Bets = y.Bets.Where(z => z.State == "Pending").Select(b => new Bet
{
Id = b.Id,
Name = b.Name,
State = b.State
}).AsQueryable()
}).AsQueryable()
}).AsQueryable();
Assuming the relations between tables are present:
var data=dbcontext.UserDataset.Include(u => u.Tipsters.Where(t => t.Visible));
should do the job

issue with select clause in linq - to - entities query

Hello I'm using code first approach and I defined the following model:
public partial class tmmodel
{
public tmmodel()
{
this.tmmodel_L10n = new HashSet<tmmodel_L10n>();
}
public int id { get; set; }
public int Order { get; set; }
public bool Active { get; set; }
public virtual ICollection<tmmodel_L10n> tmmodel_L10n { get; set; }
}
public partial class tmmodel_L10n
{
public int modelid { get; set; }
public int CultureId { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public virtual tmmodel tmmodel { get; set; }
}
So I want to get in an anonymous class - the id, Active and order property of my tmodel along with teh title in tmodel_L10:
With Linq i MANAGED TO GET the result - but when I try to use Linq - to - sql - I have some problems :
var items = from i in dc.tmmodel
join l10n in dc.tmmodel_L10n on new { i.id, cid = 1 } equals new { l10n.modelid, cid = l10n.CultureId }
select new
{
id = i.id,
i.Order,
i.Active,
l10n.Title,
};
Here is my - linq to entites query and you can see that I don't have any access to the Title property:
var linqtosqlitems = dc.tmmodel
.Include(x => x.tmmodel_L10n)
.Select(l => new {id = l.id,l.Active,**l.tmmodel_L10n.??**}).ToList();
By using include Linq Creates an Enumarable of your "child" table because it can be one to many relationship. If You are sure there is only one record in "child table" You can go like:
var linqtosqlitems = dc.tmmodel
.Include(x => x.tmmodel_L10n)
.Select(l => new {id = l.id,l.Active,l.tmmodel_L10n.FirstOrDefault().Title}).ToList();

Multiple collections in an object using Linq

I have following object
public class bizObj
{
public string name { get; set; }
public int p_id { get; set; }
public string acc_number { get; set; }
public string a_name { get; set; }
public string a_phone { get; set; }
public virtual product product { get; set; }
public virtual account account { get; set; }
}
Linq statment to get data from db is
public IEnumerable<bizObj> GetbizObj(int id)
{
var acs = (from c in db.p_account
where c.p_id==id
select new bizObj
{
name = c.p_name,
p_id = c.product.id,
acc_number=c.account.acc_number,
a_name = c.a_name,
a_phone = c.a_phone
});
return acs;
}
The above code is working fine but it is returning one collection. What I am trying to
get is that it has a collection of
{
name,
p_id
//than a second collection which has all the records that have same name ane p_id
{
acc_number,
a_name
a_phone
}
}
Please let me know how I can accomplish this using linq/lambda expression. Thanks
Question is unclear, but it looks like you're saying you want to group rows by name and p_id.
var query = acs.GroupBy(x => new { x.name, x.p_id })
.Select(g => new { g.Key.name, g.Key.p_id, Items = g.ToList() });

MVC4 C# Populating data in a viewmodel from database

I have a viewmodel which needs data from two models person and address:
Models:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Gender { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public int Zip { get; set; }
public int PersonId {get; set; }
}
The Viewmodel is as such
public class PersonAddViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Street { get; set; }
}
I have tried several ways to get data into the viewmodel and pass it to the view. There will be multiple records returned to display.
My latest method is populating the view model as such:
private AppContexts db = new AppContexts();
public ActionResult ListPeople()
{
var model = new PersonAddViewModel();
var people = db.Persons;
foreach(Person p in people)
{
Address address = db.Addresses.SingleOrDefault(a => a.PersonId == p.Id)
model.Id = p.Id;
model.Name = p.Name;
model.Street = address.Street;
}
return View(model.ToList());
}
I get an error on the Address address = db... line of "EntityCommandExecutionException was unhandled by user code.
How can you populate a view model with multiple records and pass to a view?
Final Solution:
private AppContexts db = new AppContexts();
private AppContexts dbt = new AppContexts();
public ActionResult ListPeople()
{
List<PersonAddViewModel> list = new List<PersonAddViewModel>();
var people = db.Persons;
foreach(Person p in people)
{
PersonAddViewModel model = new PersonAddViewModel();
Address address = dbt.Addresses.SingleOrDefault(a => a.PersonId == p.Id)
model.Id = p.Id;
model.Name = p.Name;
model.Street = address.Street;
}
return View(list);
}
First, EntityCommandExecutionException errors indicates an error in the definition of your entity context, or the entities themselves. This is throwing an exception because it's found the database to be different from the way you told it that it should be. You need to figure out that problem.
Second, regarding the proper way to do this, the code you've shown should work if your context were correctly configured. But, a better way would be to use Navigational properties, so long as you want to get all related records and not specify other Where clause parameters. A navigational property might look like this:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Gender { get; set; }
public virtual Address Address { get; set; }
// or possibly, if you want more than one address per person
public virtual ICollection<Address> Addresses { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public int Zip { get; set; }
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
Then you would simply say:
public ActionResult ListPeople()
{
var model = (from p in db.Persons // .Includes("Addresses") here?
select new PersonAddViewModel() {
Id = p.Id,
Name = p.Name,
Street = p.Address.Street,
// or if collection
Street2 = p.Addresses.Select(a => a.Street).FirstOrDefault()
});
return View(model.ToList());
}
For displaying lists of objects, you could use a generic view model that has a generic list:
public class GenericViewModel<T>
{
public List<T> Results { get; set; }
public GenericViewModel()
{
this.Results = new List<T>();
}
}
Have a controller action that returns, say all people from your database:
[HttpGet]
public ActionResult GetAllPeople(GenericViewModel<People> viewModel)
{
var query = (from x in db.People select x); // Select all people
viewModel.Results = query.ToList();
return View("_MyView", viewModel);
}
Then make your view strongly typed, taking in your generic view model:
#model NameSpace.ViewModels.GenericViewModel<NameSpace.Models.People>

Categories

Resources