2
\$\begingroup\$

I am trying to do a cross-database query without having to enumerate the variable t.

public List<Models.MCMessageCenter> GetMsgCtrsWithReloInfoByUserId(Guid userId)
{
    var t = GetMessageCentersByUserId(userId).ToList();

    List<Models.MCMessageCenter> result;

    using (var ctx = new TEDREntities())
    {
        result = (from n in t
                 join m in ctx.UserAccounts.AsNoTracking() on n.UserId equals m.UserAccountId
                 select new Models.MCMessageCenter
                 {
                     Id = n.Id,
                     MessageTitle = n.MessageTitle,
                     CreatedDate = n.CreatedDate,
                     UserId = n.UserId,
                     Name = m.FirstName + " " + m.LastName,
                     RelatedEntityId = n.RelatedEntityId
                 }).ToList();
    }

t could be very large so I don't want to enumerate it by turning it into a list. Is there a better, more efficient way to perform a cross database query?

Whenever I attempt to keep t as IQueryable, I get an error like "can't query two datasources at once." I can get the exact message if anyone needs.

\$\endgroup\$
3
\$\begingroup\$

You can try to replace the .ToList() by an .AsEnumerable(). AsEnumerable() makes a transition from LINQ-to-EF to LINQ-to-Objects. AsEnumerable() does not enumerate the items (lazy enumeration).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.