Mapping LinqToEntities join to ViewModel - valueinjecter

I have the following Models and ViewModels (edited for brevity):
Order Model:
OrderId
ShippingAddressId
.....
Address Model:
AddressId
.....
OrderViewModel:
Some Property from Order Model and Address Model
how can inner join two tables and map to a viewmodel ?
var query= from o in ctx.Orders
join addr in ctx.Addresses
on o.ShippingAddressId equals addr.AddressId
select new OrderViewModel.InjectFrom(o)
.InjectFrom(addr)
as OrderViewModel;
this code doesn't work .

You need to first materialize the result with ToList:
var query = (from o in ctx.Orders
join addr in ctx.Addresses
on o.ShippingAddressId equals addr.AddressId
select new { o, addr }
).ToList()
.Select(x => new OrderViewModel().InjectFrom(x.o)
.InjectFrom(x.addr)
as OrderViewModel);

Related

LINQ Error when using multiple JOIN in same statement on CRM 2011 Plug-in

Hi I am trying to join multiple entities in CRM 2011 but I get and error saying: {"'xrmsm_sessionEnrollments' entity doesn't contain attribute with Name = 'xrmsm_termsid'."}. That is correct but I am joining the entity that have that attribute.
My Linq Query:
var query2 = from e in svsContext.xrmsm_sessionEnrollmentsSet
join s in svsContext.xrmsm_sessionsSet on e.xrmsm_SessionLookup.Id equals s.xrmsm_sessionsId
join ic in svsContext.xrmsm_institutionCoursesSet on s.xrmsm_institutionCourseLookup.Id equals ic.xrmsm_institutionCoursesId
join ts in svsContext.xrmsm_term_sessionsSet on e.xrmsm_termSessionLookup.Id equals ts.xrmsm_term_sessionsId
join t in svsContext.xrmsm_termsSet on ts.xrmsm_TermLookup.Id equals t.xrmsm_termsId
where (e.xrmsm_StudentLookup.Equals(studentlookup)
&& e.xrmsm_YearLookup.Equals(entity.GetAttributeValue("xrmsm_studentlookup"))
&& ic.xrmsm_institutionCoursesId == institutionCourseGuid
&& t.xrmsm_termsId == termGuid)
select new { sessionName = s.xrmsm_sessionsName, StudentName = e.xrmsm_studentsName, StudentId = e.xrmsm_StudentLookup.Name };
My original SQL query that works on SQL Server:
SELECT en.xrmsm_currentsessionenrollmentsname
,en.xrmsm_isreadonlyname
,en.xrmsm_sessionenrollmentsaverage
,en.xrmsm_sessionenrollmentsgrade
,en.xrmsm_sessionenrollmentsid
,en.xrmsm_sessionenrollments_id
,en.xrmsm_sessionlookup as sessionid
,en.xrmsm_sessionlookupname
,en.xrmsm_sessionsname
,en.xrmsm_studentlookup AS studentid
,en.xrmsm_studentlookupname
,en.xrmsm_studentsname
,en.xrmsm_termsessionlookup
,en.xrmsm_termsessionlookupname
,en.xrmsm_withdrawal
,en.xrmsm_yearaverage
,en.xrmsm_yeargrade
,en.xrmsm_yearlookup
,en.xrmsm_yearlookupname
FROM CoseyTest_MSCRM.dbo.Filteredxrmsm_sessionEnrollments as en INNER JOIN
CoseyTest_MSCRM.dbo.Filteredxrmsm_sessions crmsessions ON
(en.xrmsm_sessionlookup = crmsessions.xrmsm_sessionsid AND en.xrmsm_yearlookup = crmsessions.xrmsm_yearlookup)
INNER JOIN Filteredxrmsm_institutionCourses institutionCourses
on crmsessions.xrmsm_institutioncourselookup = institutionCourses.xrmsm_institutioncoursesid
Inner Join Filteredxrmsm_term_sessions as termsession
on en.xrmsm_termsessionlookup = termsession.xrmsm_term_sessionsid
Inner Join Filteredxrmsm_terms as terms
on termsession.xrmsm_termlookup = terms.xrmsm_termsid
where en.xrmsm_yearlookup = '4BA07BED-3F51-E211-8359-00155D004403'
and en.xrmsm_studentlookup = 'C844AF65-5B51-E211-8359-00155D004403'
and terms.xrmsm_termsid = 'D1D107B7-4551-E211-8359-00155D004403'
and institutionCourses.xrmsm_institutioncoursesid = '2121914E-4551-E211-8359-00155D004403'
Linq provider for CRM has some limitations. You can fount it here. Check if your query violates limitations.
Try having a look at the actual query that it is generating and run that in SQL, it may help understand what is going on.
It could be that something your doing in LINQ is not available in CRM as paramosh mentioned but I can't see anything that jumps out at me.
Thanks for all your responses. I did the following workaround.
Make a query that retrieve a list of all the Student enrollments
Use the Find method of the List<T> class to find if a record exist with the conditions I am searching for.
var query2 = (from e in svsContext.xrmsm_sessionEnrollmentsSet
join s in svsContext.xrmsm_sessionsSet on e.xrmsm_SessionLookup.Id equals s.xrmsm_sessionsId
join ic in svsContext.xrmsm_institutionCoursesSet on s.xrmsm_institutionCourseLookup.Id equals ic.xrmsm_institutionCoursesId
join ts in svsContext.xrmsm_term_sessionsSet on e.xrmsm_termSessionLookup.Id equals ts.xrmsm_term_sessionsId
join t in svsContext.xrmsm_termsSet on ts.xrmsm_TermLookup.Id equals t.xrmsm_termsId
where (e.xrmsm_StudentLookup.Equals(studentlookup))
select new
{
EnrollmentId = e.xrmsm_sessionEnrollments_id,
SessionId = s.xrmsm_sessions_id,
EnrollmentYear = e.xrmsm_YearLookup,
InstitutionCourseId = (Guid)ic.xrmsm_institutionCoursesId,
TermId = (Guid)t.xrmsm_termsId,
Student = e.xrmsm_StudentLookup
}).ToList();
var q = query2.Find(r => r.EnrollmentYear.Id == entity.GetAttributeValue<EntityReference>("xrmsm_yearlookup").Id
&& r.InstitutionCourseId == institutionCourseGuid
&& r.TermId == termGuid);

linq to entities assigning join data to viewmodel

Here is a linq query that assigns data to a viewmodel. It works great.
var data = (from C in db.CaseComplaints
where C.CasesID == caseid
select C.ComplaintCode).ToList().Select(x => new CaseComplaintsViewModel()
{
ComplaintCode = x.ComplaintCodeName,
ComplaintType = x.ComplaintType
}).ToList();
Here is code that tries to perform a join and assign the data to a viewmodel. It does not work. the code editor tells me that x does not have a method or definition for BranchName
var data = (from branch in db.Branches
join customer in db.Customers
on branch.BranchID equals customer.BranchID
where customer.BranchID == bid
select branch.BranchName).ToList().Select(x => new CaseResponsibleBranchViewModel()
{
BranchName = x.BranchName
});
What am I missing?
You've already selected BranchName, which I suppose is a string, so there's no BranchName property of x:
var data = (from branch in db.Branches
join customer in db.Customers
on branch.BranchID equals customer.BranchID
where customer.BranchID == bid
select branch.BranchName).ToList().Select(x => new CaseResponsibleBranchViewModel()
{
BranchName = x
});

The .NET Standard Query Operators - emptyOrder

In this msdn example:
The .NET Standard Query Operators
We see this example of a LEFT JOIN:
var custTotalOrders =
from c in customers
join o in orders on c.CustomerID equals o.CustomerID into co
from o in co.DefaultIfEmpty(emptyOrder)
select new { c.Name, o.OrderDate, o.Total };
And it says that:
where emptyOrder is an Order instance used to represent a missing order.
So, where does emptyOrder come from?
How can I use it in my code?
There are two overloads for DefaultIfEmpty
DefaultIfEmpty()
DefaultIfEmpty(defaultValue)
The first will return default(T) where T is the type contained in the enumerable when the enumerable is empty.
The second will return the value given in the defaultValue parameter when the enumerable is empty.
Assuming you declared emptyOrder like so:
Order emtpyOrder = new Order() { Total=100 };
Then in the query:
var custTotalOrders =
from c in customers
join o in orders on c.CustomerID equals o.CustomerID into co
from o in co.DefaultIfEmpty(emptyOrder)
select new { c.Name, o.OrderDate, o.Total };
When a customer did not have a matching order, the Total property in the projected anonymous object would have the value 100.

How to return specific list result from linq to entities

Hi Experts
I currenly write this code:
public IList<TResult> GetCustomQuery<TResult>(int orderID, Func<Order, TResult> selector)
{
using(RepositoryDataContext = new NorthwindEntities())
{
IList<TResult> res = (from od in RepositoryDataContext.Order_Details
join o in RepositoryDataContext.Orders
on od.OrderID equals o.OrderID
join p in RepositoryDataContext.Products
on od.ProductID equals p.ProductID
join c in RepositoryDataContext.Customers
on o.CustomerID equals c.CustomerID
where o.OrderID > orderID
select new
{
o.OrderID,
od.UnitPrice,
od.Quantity,
p.ProductName,
c.CompanyName
}).Select<Order, TResult>(selector).ToList();
}
}
I want to return result of my linq to entities in specific format(TResult).
writing this code using Lambda Expression is hard because of joins.
this line has exception :Select < Order, TResult > (selector)
how I can Fix that?
thanks
Instead of creating an Anonymous type, create a POCO or in your case an Order
select new Order()
{
OrderProperty1 = o.OrderID,
OrderProperty2 = od.UnitPrice,
OrderProperty3 = od.Quantity,
OrderProperty4 = p.ProductName,
OrderProperty5 = c.CompanyName
}).Select<Order, TResult>(selector).ToList();
In your second last line: Try to call ToList() before the Select(). Like this:
}).ToList().Select<Order, TResult>(selector);
Is it possible Iterate "res" and return a list of TResult?

LINQ to pull back object graph

Is it possible to group by more than one group in LINQ?
For instance, I have this query (obviously incomplete)
from lxr in LOCATION_XREFs
join l in LOCATIONs on lxr.LOCATION_SKEY equals l.LOCATION_SKEY
join c in COMPANies on l.COMPANY_SKEY equals c.COMPANY_SKEY
join prlx in PeopleRoleLocationXrefs on lxr.LOCATION_XREF_SKEY equals prlx.LOCATION_XREF_SKEY
join p in PEOPLEs on prlx.PEOPLE_SKEY equals p.PEOPLE_SKEY
join pr in PeopleRoles on prlx.PeopleRoleKey equals pr.PeopleRoleKey
... and I'd like to get back a bunch of companies that have a bunch of locations that, in turn, have a bunch of people.
If you're looking to group by multiple parameters, that is possible:
from lxr in LOCATION_XREFs
join l in LOCATIONs on lxr.LOCATION_SKEY equals l.LOCATION_SKEY
join c in COMPANies on l.COMPANY_SKEY equals c.COMPANY_SKEY
join prlx in PeopleRoleLocationXrefs on lxr.LOCATION_XREF_SKEY equals prlx.LOCATION_XREF_SKEY
join p in PEOPLEs on prlx.PEOPLE_SKEY equals p.PEOPLE_SKEY
join pr in PeopleRoles on prlx.PeopleRoleKey equals pr.PeopleRoleKey
group c by new { c.COMPANY_SKEY, l.LOCATION_SKEY} into myGroup
select new
{
myGroup.Key.COMPANY_SKEY,
myGroup.Key.LOCATION_SKEY,
myGroup.Count()
}
I ultimately used the following code to achieve what I wanted. Interesting note: notice that I wanted the Roles flattened out and concatenated with commas, so I did this using a wrapper accessor that uses the Aggregate operator. Apparently the Aggregate operator is not suuported within a LINQ query, as you get "the query operator "Aggregate" is not supported" otherwise I would have done it right there inline like this Roles = new List<string>(g.Select(u => u.pr.RoleName)).Aggregate((a, b) => a + ", " + b)
public class DAOPerson
{
public string Name { get; set; }
public int PeopleSkey { get; set; }
public List<string> RolesCollection { get; set; }
public string Roles
{
get { return RolesCollection.Aggregate((a, b) => a + ", " + b); }
}
}
IQueryable<DAOLocation> gridData;
gridData = (from lxr in db.LOCATION_XREFs
join l in db.LOCATIONs on lxr.LOCATION_SKEY equals l.LOCATION_SKEY
join c in db.COMPANies on l.COMPANY_SKEY equals c.COMPANY_SKEY
where lxr.DEPARTMENT_NUMBER == Department.BINDING_AUTHORITY_KEY
&& lxr.BRANCH_NUMBER == Branch.ATLANTAKEY
orderby c.NAME, l.INTERNAL_NAME ascending
select new DAOLocation
{
CompanyName = c.NAME,
CompanySkey = c.COMPANY_SKEY,
LocationName = l.INTERNAL_NAME,
LocationSkey = l.LOCATION_SKEY,
Persons = (from prlx in db.PeopleRoleLocationXrefs
join lxr2 in db.LOCATION_XREFs on prlx.LOCATION_XREF_SKEY equals lxr.LOCATION_XREF_SKEY
join p in db.PEOPLEs on prlx.PEOPLE_SKEY equals p.PEOPLE_SKEY
join pr in db.PeopleRoles on prlx.PeopleRoleKey equals pr.PeopleRoleKey
where lxr2.LOCATION_SKEY == l.LOCATION_SKEY
group new { p, pr } by p.PEOPLE_SKEY into g
select new DAOPerson
{
Name = g.First().p.LAST_NAME,
PeopleSkey = g.First().p.PEOPLE_SKEY,
RolesCollection = new List<string>(g.Select(u => u.pr.RoleName))
}).ToList()
});

Resources