So I have the following code and I want to create a list of different food that is saved in the class TRoom.
Food = class
durability : Integer;
end;
TRoom = class
Food_list : ???
end;
Example:
sleeping: TRoom
and in "sleeping" is an apple and a banana both are objects.
Thanks for your help
Nuraxx
Related
I try to reduce SQL queries from my Rails application.
I have some controller like:
class Rest::MyController < Rest::BaseController
def show
render xml: some_model, status: :ok
end
private
def my_associations
[
:model2,
:model3,
:model4,
]
end
def some_model
#some_model ||= SomeModel.includes(my_associations).where(id: test_params[:id])
end
def test_params
params.permit(:id)
end
end
To avoid N + 1 I use includes, so basically when i try to execute some_model method, AR make lot of call's like that (SELECT ALL FROM):
SomeModel Load (1.7ms) SELECT `model2`.* FROM `model2` WHERE `model2`.`type` IN ('SomeModel') AND `model2`.`is_something` = 0 AND `model2`.`id` = 1
SomeModel Load (1.7ms) SELECT `model3`.* FROM `model3` WHERE `model3`.`type` IN ('SomeModel') AND `model3`.`is_something` = 0 AND `model3`.`id` = 1
SomeModel Load (1.7ms) SELECT `model4`.* FROM `model4` WHERE `model4`.`type` IN ('SomeModel') AND `model4`.`is_something` = 0 AND `model4`.`id` = 1
This is only example
Now, through my serializer I would like to get only selected columns for model2, model3 and model4
Unfortunately Active record make a call like SELECT model2.* FROM
For example, for model2 serializer i try to get only (:id, :name) columns.
Is it possible to make a call like ?
SELECT some_model.*, model2.id, model2.name FROM `some_model`
instead
SELECT `model2`.* FROM `model2` WHERE `model2`.`type` IN ('SomeModel')
If you want to use Rails's includes feature, then no, there isn't an especially good way to selectively control the columns from included models.
If you're looking for help to optimize the query, you'll need to provide more specifics about the data schema and the desired output.
I want to call controller method for data in that there can be multiple data output from different table so it depends on user selection from which table they wanna to take output and so i was thinking to get output using single linq just by passed name of table according to user selection.
query = (from Ledger in fen.TblLedgers
join acc in fen.Accounts
on Ledger.Code equals acc.AccId
where acc.DrCr == Fin.DRCR && Ledger.Grp == Fin.LedGrp && acc.Vdate <= DateTime.Today && Ledger.isActive == "Y" group acc by new { acc.AccId } into g select g.Sum(acc => acc.Amt)).Sum() ;
If you want to do this, one approach would be to use lambdas and implement something like the below.
Long and short of it - you'd need to leverage the DbContext.Set < T > method DbContext.Set method (MSDN) or the generic one (MSDN)
Vary T (or the type parameter) by the users choice, and then lambda from there.
However, in order to do this, all T's would need to implement the same interface and you'd need to cast the elements to that interface before using them. But, you'd have to do this if you want any of this stuff to work at all...)
(Code below written freehand, don't complain if there's the odd mistake!)
So - with classes:
public interface IAnimal
{
....
}
public class Dog :IAnimal
{
....
}
public class Cat :IAnimal
{
....
}
public class Cow :IAnimal
{
...
}
You could use something like:
var userSelection = "Dog";
Type chosenType = chooseTypeAccordingToUserSelection(userSelection);
var animalDBSet = GetOpenDBcontext().Set(chosenType).Cast<IAnimal>();
var insects = animalDBSet.Where(a=>a.NumberOfLegs>4);
HTH!
All,
I am experimenting with Linq and Entity Framework (athough I am using nHydrate) in VB.NET. Lets say I have 3 tables as follows:
So Table1 is the top level grandparent which has a number of records/entities.
I want to select all of Table3 records/entities that are related to a specific Table1 instance. I want to do this as part of some search functionality.
I want to take an instance of Table1 as my starting point, i.e.
Public Class MySearch
Private _lookUnder As System.Data.Objects.DataClasses.EntityObject
Public Sub Search()
...
CType(_lookUnder, Table1) ' ???? need to linq here ????
...
End Sub
End Class
Can this be done completely with Linq? C# answers are welcome.
I've tried looking at this but didn't help too much.
Thanks,
Andez
Ideally, you should have access to the context, making something like this possible (C#):
var relatedTable3s = context.Table3s.Where(t3 => t3.Table2.Table1.id == _lookUnder.id);
I suppose something like this might work, too:
var relatedTable3s =
from t2 in _lookUnder.Table2s.AsQueryable()
from t3 in t2.Table3s
select t3;
... otherwise written as:
var relatedTable3s = _lookUnder.Table2s.AsQueryable()
.SelectMany(t2 => t2.Table3s);
I have a collection of different objects that derive from the same parent.
How can I extract objects of a particular type from a collection containing mixed types
e.g.
public class A {}
public class B : A {}
public class C : A {}
The collection will contain objects of type B and C
I'm halfway there just need help filling in the '[]' bit
var x = from xTypes in xCollection where '[type of object is type B]' select xTypes;
Thanks.
You should use the OfType<T> extension method rather than LINQ query syntax for this:
var x = xCollection.OfType<B>();
This will give you back an IEnumerable<B>. If you do want to use the LINQ query syntax, you'd have to do this:
var x = from obj in xCollection where obj is B select (B)obj;
var x = from xTypes in xCollection
where xTypes is B
select xTypes;
or if you want exactly this type and not any derived types:
var x = from xTypes in xCollection
where xTypes.GetType() == typeof(B)
select xTypes;
I'm trying to retrieve a list of Id's from a collection that is a few levels deep in an object heirachy. When i try to do a ToList(), I keep getting an EntityList<> retrieved instead .. which means it's not allowing me to retrieve an instance's BarId property because the EntitySet is a Enumerable, not a single instance object.
Foo.Child1 (1 to 1)
Child1.Children2 (0 to many of type Bar)
Bar.BarId int;
IList<Foo> fooList = (from blah blah blah).ToList();
var children2List = (from x in fooList
select x.Child1.Children2).ToList();
It keeps returning children2List as an EntitySet<Bar>, not an IList<Bar>. As such, i'm struggling to retrieve the list of BarId's from children2List.
please help!
Your can use:
var children2List = fooList.SelectMany( x => x.Child1.Children2 ).ToList();
This will allow you to do something like:
children2List.ForEach( b => b.BarId.Print() );
In your query, you turn the whole result into a list, not the individual Children2 sets.
Try
var children2List = (from x in fooList
select x.Child1.Children2.ToList()).ToList();
This will turn each Children2 into a List.
EntitySet<T> implements IList<T>, so you already are returning IList<Bar>.