I do have a table "Reference" and a table "Article" where a article references other articles.
I do have simple references like:
A -> B
SQL:
select ab.*
from Article a
inner join Reference ab on ab.ArticleFromId = a.Id
inner join Article b on b.Id = ab.ArticleToId
where a.ArticleNo = "1234"
C# LINQ:
_context.Reference
.Where(r => r.ArticleFromNavigation.ArticleNo.Equals("1234"));
I also do have reference-chains like: A -> B -> C
(Lets assume there's only a maximum of 3 articles in a chain)
SQL:
select ab.ArticleFromId, bc.ArticleToId
from Article a
inner join Reference ab on ab.ArticleFromId = a.Id
inner join Article b on b.Id = ab.ArticleToId
inner join Reference bc on bc.ArticleFromId = b.Id
inner join Article c on c.Id = bc.ArticleToId
where a.ArticleNo = "1234"
This is easy in SQL, as the result just multiplies with the additional joins, but I don't know how to write that in LINQ.
I want it to be something like this (which wont work):
_context.Reference
.Where(r => r.ArticleFromNavigation.ArticleNo.Equals("1234"))
.Select(r => new Reference
{
ArticleFromNavigation = r.ArticleFromNavigation, //this is article "A"
ArticleToNavigation = r.ArticleToNavigation.ReferenceArticleToNavigations //this wont work as it's a iCollection
}).AsNoTrackable();
Here I want new results of the type "Reference" for "A -> C".
I guess I have to include/theninclude/join/select/selectmany(?) the collection before the "new Reference"-section, but I have no clue.
Is there any way I can archive that?
Well, you can do it exactly as in SQL, but use navigation properties instead of joins.
I'll use LINQ query syntax because it better shows the similarity, and also method syntax is quite convoluted and hard to read for such type of queries:
from a in _context.Article
from ab in a.ReferenceArticleFromNavigations
let b = ab.ArticleToNavigation
from bc in b.ReferenceArticleFromNavigations
let c = bc.ArticleToNavigation
where a.ArticleNo = "1234"
select new Reference
{
ArticleFromNavigation = a,
ArticleToNavigation = c,
}
The let statements are not strongly needed (you could use reference navigation property directly), I've included them just to make the LINQ query closer to SQL query.
Actually the method equivalent is not that bad in this case - flatten several levels with nested SelectMany and project the (top, bottom) pair using the SelectMany overload allowing that:
_context.Article
.Where(a => a.ArticleNo = "1234")
.SelectMany(a => a.ReferenceArticleFromNavigations
.SelectMany(ab => ab.ArticleToNavigation.ReferenceArticleFromNavigations)
// include as many `SelectMany` like the above as you wish until you hit the desired level of nesting
.Select(bc => bc.ArticleToNavigation),
(a, c) => new Reference
{
ArticleFromNavigation = a,
ArticleToNavigation = c,
});
I modeled the database as classes to get syntax correct. See code below :
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication107
{
class Program
{
static void Main(string[] args)
{
Context _context = new Context();
string ArticleNo = "1234";
var results = (from a in _context.article.Where(x => x.Id == ArticleNo)
join ab in _context.reference
.Where(x => (x.ArticleFromId == x.ArticleToId))
on a.Id equals ab.ArticleFromId
select new { a = a, ab = ab }
).Select(r => new Reference()
{
ArticleFromNavigation = r.a,
ArticleToNavigation = r.a.ReferenceArticleToNavigations.ToList()
}).ToList();
}
}
public class Context
{
public List<Reference> reference { get; set; }
public List<Article> article { get; set; }
}
public class Reference
{
public string ArticleFromId { get; set; }
public string ArticleToId { get; set; }
public Article ArticleFromNavigation { get; set; }
public List<string> ArticleToNavigation { get; set; }
}
public class Article
{
public string Id { get; set; }
public List<string> ReferenceArticleToNavigations { get; set; }
}
}
SETUP
I have an "Order" class and an "OrderDetail" class.
The Order class has a property called "OrderDetails" of type IQueryable.
I have a method in each class called "GetData" and this method returns IQueryable of the class it is in.
The "GetData" methods are wrappers around the Entities that are used to transform the Entities into friendlier .NET classes / properties.
For example: The OrderDetail Entity has a "PullDate" in the database as Char(10) but I want the OrderDetail Class to have a property called "PullDate" that is of type DateTime in the class.
CODE
public class Order
{
[Key]
public int ID { get; set; }
public IQueryable<OrderDetail> OrderDetails { get; set; }
public static IQueryable<Order> GetData()
{
IQueryable<Order> orders;
var db = new OrderEntities();
// NOTE: This code will work
try
{
var data =
(from O in db.Orders
where O.OrderID == 1
select new Order
{
ID = O.OrderID,
OrderDetails =
(from OD in db.OrderDetails
where OD.OrderID == O.OrderID
select new OrderDetail
{
ID = OD.OrderDetailID,
OrderID = OD.OrderID,
PullDate = OD.PullDate == "00000000" ?
(DateTime?)null : db.yyyyMMddToDate(OD.PullDate),
Description = OD.Desc
})
});
orders = data.AsQueryable();
var orderList = orders.ToList();
}
catch (Exception ex)
{
throw;
}
// NOTE: This code will NOT work
try
{
var data = (from O in db.Orders
where O.OrderID == 1
select new Order
{
ID = O.OrderID,
OrderDetails = (from OD in OrderDetail.GetData(db)
where OD.OrderID == O.OrderID
select OD)
});
orders = data.AsQueryable();
var orderList = orders.ToList();
}
catch (Exception ex)
{
throw;
}
return orders;
}
}
public class OrderDetail
{
[Key]
public int ID { get; set; }
public int OrderID { get; set; }
public DateTime? PullDate { get; set; }
public string Description { get; set; }
public static IQueryable<OrderDetail> GetData(OrderEntities db)
{
IQueryable<OrderDetail> orderDetails;
var data = (from OD in db.OrderDetails
select new OrderDetail
{
ID = OD.OrderDetailID,
OrderID = OD.OrderID,
PullDate = OD.PullDate == "00000000" ?
(DateTime?)null : db.yyyyMMddToDate(OD.PullDate),
Description = OD.Desc
});
orderDetails = data.AsQueryable();
var orderList = orderDetails.ToList();
return orderDetails;
}
}
ERROR
LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[Models.OrderDetail] GetData(Models.OrderEntities)' method, and this method cannot be translated into a store expression.
Request
I would like the Order.GetData method to call the OrderDetail.GetData method using LINQ.
I need to "join" the GetData methods together or "sub select" the OrderDetail.GetData while inside the Order.GetData class.
Both of the classes are querying EntityFramework inside of their GetData methods.
Projection is a requirement.
My goal is to create "Reusable" methods like "GetData" in my DTO classes that will contain specific SQL / Entity logic.
For example, I am using a lot of custom SQL functions like "db.yyyyMMddToDate" in my DTO classes to transform the Entities into something more object / .NET friendly and I don't want to "retype" all that logic each time I need to "join / sub select" data from entities.
In LINQ to Objects this would be the same as joining two different lists from different classes.
But it seems that LINQ to Entity does not know how to join methods from other classes even if the method is marked as Queryable.
I understand that LINQ to Entity is treating the "GetData" methods as if they were SQL functions but I need a way to tell LINQ to Entity that these are just more Entities to join together to get the results.
You get this error because, when dealing with LINQ to Entities, you are not passing in lambdas to the LINQ operators like you do in LINQ to Object. You are passing expression trees. The code that you write in those queries will never be executed. It will be interpreted by Entity Framework as a series of instruction to translate into an other language like SQL.
The GetData method doesn't mean anything in SQL. In fact, it doesn't mean anything outside of your own program. This is why LINQ to Entities do not recognize your method as a valid construct.
To access the details of an order in a scenario like yours, you need to build your query so that EF knows to include the OrderDetails entities inside the primary query using the Include operator. When doing this, EF expects your object to have a property similar to this one:
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
This is how EF will know where to put the resulting objects from queries that includes those elements.
var ctx = new DatabaseContext();
var query = ctx.Orders
.Where(o => o.OrderId == 1)
.Include(o => o.OrderDetails);
This query will asks the remote server for all Orders, with an OrderId of 1, with all its matching OrderDetails. EF will then automatically create all of the related client side entities for you.
Here's the official documentation on EF about this topic: http://msdn.microsoft.com/en-ca/data/jj574232.aspx
Is It possible to populate a List from within a EF query? My DTO is as follows:
public DTO()
{
public string ClientName { get; set;}
public List<string> RelatedCodes { get; set;}
}
My method to populate DTO =
Public DTO MyResult(string ClientCode)
{
return (from o in repository.RelatedClient
where o.LeadCode == ClientCode
select new DTO { ClientName = o.ClientName, RelatedCodes = o.RelatedCodes.ToList()});
}
I know I shouldn't be adding the ToList() within the query or if I can I am doing it very wrong. Can anyone advise how this is done. My DTO is cut down for this example but I want to have lots of single properties and a List<> (or several Lists) in a DTO with one Entity query if possible?
So following answer below I presume there isnt a way to query this direct. My original option would have been:
Public DTO MyResult(string ClientCode)
{
var temp = (from o in repositry.RelatedCode Where o.LeadCode == ClientCode select o).ToList();
DTO dto = (from o in repository.LeadClient Where o.LeadCode == ClientCode Select o.ClientName).Firstordefault();
foreach(string rc in temp)
{ dto.RelatedCode.Add(rc);}
return dto;
}
I am happy with this but would be grateful if someone could advise if this is the correct way to deal without something like automapper. I am trying to learn Entity Framework and want to check I am not missing some built in functionality.
Something like:
var dtoEntity = repo.RelatedClient.SingleOrDefault(x => x.LeadCode == ClodeCode);
DTO d = new DTO();
d.ClientName = dtoEntity.ClientName;
foreach (var relatedCode in dtoEntity.RelatedCodes)
{
d.RelatedCodes.Add(relatedCode);
}
return d;
Using this approach you can add as many properties as you'd like. However, I'd suggest using AutoMapper for this.
Read more here:
http://nuget.org/packages/automapper
I'm using ActiveRecord on Subsonic 3 and I effectively want to do this:
select * from foo
left outer join bar on bar.Id = foo.barId
where foo.someProperty = 2
I've written a stored procedure to fetch the data but Subsonic has only created objects to hold the columns from foo and bar.
What's the best way of returning the data into a single object so I can just bind it. Ideally I want it to be in a list<> but without writing my own class, there doesn't seem to be a way provided by subsonic.
You have a couple options here...
You could create a database view that does your join, and have SubSonic generate a data type for your view, then your select would be just like selecting from any other table.
Alternatively, you could use a Linq expression to do the join into an anonymous or dynamic type (if you are using .net 4) For example:
public List<dynamic> LoadData(int id)
{
var data = from f in db.Foo
from b in db.Bar.Where(x => x.Id == f.BarId).DefaultIfEmpty()
where f.SomeProperty == id
select new
{
SomeProperty = f.Something,
AnotherProperty = b.SomethingElse
};
return data.Cast<dynamic>().ToList();
}
Of course another alternative is to do the Linq expression above, but define your own class to hold the returned data, and select into it.
public class MyData
{
public string SomeProperty { get; set; }
public string AnotherProperty { get; set; }
}
public List<MyData> LoadData(int id)
{
var data = from f in db.Foo
from b in db.Bar.Where(x => x.Id == f.BarId).DefaultIfEmpty()
where f.SomeProperty == id
select new MyData()
{
SomeProperty = f.Something,
AnotherProperty = b.SomethingElse
};
return data.ToList();
}
There is an entity type called Product that is generated by entity framework.
I have written this query
public IQueryable<Product> GetProducts(int categoryID)
{
return from p in db.Products
where p.CategoryID== categoryID
select new Product { Name = p.Name};
}
The code below throws the following error :
"The entity or complex type Shop.Product cannot be constructed in a
LINQ to Entities query"
var products = productRepository.GetProducts(1).Tolist();
But when I use select p instead of select new Product { Name = p.Name}; it works correctly.
How can I preform a custom select section?
You cannot (and should not be able to) project onto a mapped entity. You can, however, project onto an anonymous type or onto a DTO:
public class ProductDTO
{
public string Name { get; set; }
// Other field you may need from the Product entity
}
And your method will return a List of DTO's.
public List<ProductDTO> GetProducts(int categoryID)
{
return (from p in db.Products
where p.CategoryID == categoryID
select new ProductDTO { Name = p.Name }).ToList();
}
You can project into anonymous type, and then from it to model type
public IEnumerable<Product> GetProducts(int categoryID)
{
return (from p in Context.Set<Product>()
where p.CategoryID == categoryID
select new { Name = p.Name }).ToList()
.Select(x => new Product { Name = x.Name });
}
Edit: I am going to be a bit more specific since this question got a lot of attention.
You cannot project into model type directly (EF restriction), so there is no way around this. The only way is to project into anonymous type (1st iteration), and then to model type (2nd iteration).
Please also be aware that when you partially load entities in this manner, they cannot be updated, so they should remain detached, as they are.
I never did completely understand why this is not possible, and the answers on this thread do not give strong reasons against it (mostly speaking about partially loaded data). It is correct that in partially loaded state entity cannot be updated, but then, this entity would be detached, so accidental attempts to save them would not be possible.
Consider method I used above: we still have a partially loaded model entity as a result. This entity is detached.
Consider this (wish-to-exist) possible code:
return (from p in Context.Set<Product>()
where p.CategoryID == categoryID
select new Product { Name = p.Name }).AsNoTracking().ToList();
This could also result in a list of detached entities, so we would not need to make two iterations. A compiler would be smart to see that AsNoTracking() has been used, which will result in detached entities, so it could allow us to do this. If, however, AsNoTracking() was omitted, it could throw the same exception as it is throwing now, to warn us that we need to be specific enough about the result we want.
There is another way that I found works, you have to build a class that derives from your Product class and use that. For instance:
public class PseudoProduct : Product { }
public IQueryable<Product> GetProducts(int categoryID)
{
return from p in db.Products
where p.CategoryID== categoryID
select new PseudoProduct() { Name = p.Name};
}
Not sure if this is "allowed", but it works.
Here is one way to do this without declaring aditional class:
public List<Product> GetProducts(int categoryID)
{
var query = from p in db.Products
where p.CategoryID == categoryID
select new { Name = p.Name };
var products = query.ToList().Select(r => new Product
{
Name = r.Name;
}).ToList();
return products;
}
However, this is only to be used if you want to combine multiple entities in a single entity. The above functionality (simple product to product mapping) is done like this:
public List<Product> GetProducts(int categoryID)
{
var query = from p in db.Products
where p.CategoryID == categoryID
select p;
var products = query.ToList();
return products;
}
Another simple way :)
public IQueryable<Product> GetProducts(int categoryID)
{
var productList = db.Products
.Where(p => p.CategoryID == categoryID)
.Select(item =>
new Product
{
Name = item.Name
})
.ToList()
.AsQueryable(); // actually it's not useful after "ToList()" :D
return productList;
}
You can use this and it should be working --> You must use toList before making the new list using select:
db.Products
.where(x=>x.CategoryID == categoryID).ToList()
.select(x=>new Product { Name = p.Name}).ToList();
In response to the other question which was marked as duplicate (see here) I figured out a quick and easy solution based on the answer of Soren:
data.Tasks.AddRange(
data.Task.AsEnumerable().Select(t => new Task{
creator_id = t.ID,
start_date = t.Incident.DateOpened,
end_date = t.Incident.DateCLosed,
product_code = t.Incident.ProductCode
// so on...
})
);
data.SaveChanges();
Note:
This solution only works if you have a navigation property (foreign key) on the Task class (here called 'Incident').
If you don't have that, you can just use one of the other posted solutions with "AsQueryable()".
You can solve this by using Data Transfer Objects (DTO's).
These are a bit like viewmodels where you put in the properties you need and you can map them manually in your controller or by using third-party solutions like AutoMapper.
With DTO's you can :
Make data serialisable (Json)
Get rid of circular references
Reduce networktraffic by leaving properties you don't need (viewmodelwise)
Use objectflattening
I've been learning this in school this year and it's a very useful tool.
If you are using Entity framework, then try removing property from DbContext which uses your complex model as Entity
I had same problem when mapping multiple model into a viewmodel named Entity
public DbSet<Entity> Entities { get; set; }
Removing the entry from DbContext fixed my error.
if you are Executing Linq to Entity you can't use the ClassType with new in the select closure of query only anonymous types are allowed (new without type)
take look at this snippet of my project
//...
var dbQuery = context.Set<Letter>()
.Include(letter => letter.LetterStatus)
.Select(l => new {Title =l.Title,ID = l.ID, LastModificationDate = l.LastModificationDate, DateCreated = l.DateCreated,LetterStatus = new {ID = l.LetterStatusID.Value,NameInArabic = l.LetterStatus.NameInArabic,NameInEnglish = l.LetterStatus.NameInEnglish} })
^^ without type__________________________________________________________________________________________________________^^ without type
of you added the new keyword in Select closure even on the complex properties you will got this error
so remove the ClassTypes from new keyword on Linq to Entity queries ,,
because it will transformed to sql statement and executed on SqlServer
so when can I use new with types on select closure?
you can use it if you you are dealing with LINQ to Object (in memory collection)
//opecations in tempList , LINQ to Entities; so we can not use class types in select only anonymous types are allowed
var tempList = dbQuery.Skip(10).Take(10).ToList();// this is list of <anonymous type> so we have to convert it so list of <letter>
//opecations in list , LINQ to Object; so we can use class types in select
list = tempList.Select(l => new Letter{ Title = l.Title, ID = l.ID, LastModificationDate = l.LastModificationDate, DateCreated = l.DateCreated, LetterStatus = new LetterStatus{ ID = l.LetterStatus.ID, NameInArabic = l.LetterStatus.NameInArabic, NameInEnglish = l.LetterStatus.NameInEnglish } }).ToList();
^^^^^^ with type
after I executed ToList on query it became in memory collection so we can use new ClassTypes in select
In many cases, the transformation is not needed. Think for the reason you want the strongly type List, and evaluate if you just want the data, for example, in a web service or for displaying it. It does not matter the type.
You just need to know how to read it and check that is identical to the properties defined in the anonymous type that you defined. That is the optimun scenario, cause something you don't need all the fields of an entity, and that's the reason anonymous type exists.
A simple way is doing this:
IEnumerable<object> list = dataContext.Table.Select(e => new { MyRequiredField = e.MyRequiredField}).AsEnumerable();
It won't let you map back onto Product since that is your table you are querying. You need an anonymous function, then you can add it to a ViewModel, and add each ViewModel to a List<MyViewModel> and return these. It's a slight digression, but I include caveats about handling nullable dates because these are a pain in the behind to deal with, just in case you have any. This is how I handled it.
Hopefully you have a ProductViewModel:
public class ProductViewModel
{
[Key]
public string ID { get; set; }
public string Name { get; set; }
}
I have dependency injection/repository framework where I call a function to grab my data. Using your post as an example, in your Controller function call, it would look like this:
int categoryID = 1;
var prods = repository.GetProducts(categoryID);
In the repository class:
public IEnumerable<ProductViewModel> GetProducts(int categoryID)
{
List<ProductViewModel> lstPVM = new List<ProductViewModel>();
var anonymousObjResult = from p in db.Products
where p.CategoryID == categoryID
select new
{
CatID = p.CategoryID,
Name = p.Name
};
// NOTE: If you have any dates that are nullable and null, you'll need to
// take care of that: ClosedDate = (DateTime?)p.ClosedDate ?? DateTime.Now
// If you want a particular date, you have to define a DateTime variable,
// assign your value to it, then replace DateTime.Now with that variable. You
// cannot call a DateTime.Parse there, unfortunately.
// Using
// new Date("1","1","1800");
// works, though. (I add a particular date so I can edit it out later.)
// I do this foreach below so I can return a List<ProductViewModel>.
// You could do: return anonymousObjResult.ToList(); here
// but it's not as clean and is an anonymous type instead of defined
// by a ViewModel where you can control the individual field types
foreach (var a in anonymousObjResult)
{
ProductViewModel pvm = new ProductViewModel();
pvm.ID = a.CatID;
pvm.Name = a.Name;
lstPVM.Add(rvm);
}
// Obviously you will just have ONE item there, but I built it
// like this so you could bring back the whole table, if you wanted
// to remove your Where clause, above.
return lstPVM;
}
Back in the controller, you do:
List<ProductViewModel> lstProd = new List<ProductViewModel>();
if (prods != null)
{
// For setting the dates back to nulls, I'm looking for this value:
// DateTime stdDate = DateTime.Parse("01/01/1800");
foreach (var a in prods)
{
ProductViewModel o_prod = new ReportViewModel();
o_prod.ID = a.ID;
o_prod.Name = a.Name;
// o_prod.ClosedDate = a.ClosedDate == stdDate ? null : a.ClosedDate;
lstProd.Add(o_prod);
}
}
return View(lstProd); // use this in your View as: #model IEnumerable<ProductViewModel>
only add AsEnumerable() :
public IQueryable<Product> GetProducts(int categoryID)
{
return from p in db.Products.AsEnumerable()
where p.CategoryID== categoryID
select new Product { Name = p.Name};
}
you can add AsEnumerable to your collection like the follow :
public IQueryable<Product> GetProducts(int categoryID)
{
return from p in db.Products.AsEnumerable()
where p.CategoryID== categoryID
select new Product { Name = p.Name};
}