The nested query is not supported. Operation1='UnionAll' Operation2='MultiStreamNest' - linq

I have a Linq to Entities query of the following form:
var x = from a in SomeData
where ... some conditions ...
select new MyType
{
Property = a.Property,
ChildCollection = from b in a.Children
select new MyChildType
{
SomeProperty = b.Property,
AnotherProperty = b.AnotherProperty
}
};
var y = from a in SomeData
where ... some other conditions ...
select new MyType
{
Property = a.Property,
ChildCollection = from b in a.Children
select new MyChildType
{
SomeProperty = b.Property,
AnotherProperty = b.AnotherProperty
}
};
var results = x.Concat(y);
(This is a simplified example - the 'where' and 'select' clauses are more complex than shown here. I'm using separate queries statements as creating a single combined one is just too complicated, has too many conditionals and takes an age to compile)
Compiles fine, but fails at execution with the exception:
"The nested query is not supported. Operation1='UnionAll' Operation2='MultiStreamNest'
Note, I am trying to project into a nested typed structure. If I call .ToList() on x and y prior to the Concat() it works fine. As a further point, one of my properties is an enum, but I am assigning to it using an integer wrapper property.
Is there a way I can do what I want to do without having to pull all the data into memory? Or is it the enum causing the failure?
Thanks,
T

Have you tried with
var results = x.Union(y);
?
Tiz
or
var x = (from a in SomeData
where ... some conditions ...
select new MyType
{
Property = a.Property,
ChildCollection = (from b in a.Children
select new MyChildType
{
SomeProperty = b.Property,
AnotherProperty = b.AnotherProperty
}).ToArray() //or DefaultIfEmpty
}).Concat(
from a in SomeData
where ... some other conditions ...
select new MyType
{
Property = a.Property,
ChildCollection = (from b in a.Children
select new MyChildType
{
SomeProperty = b.Property,
AnotherProperty = b.AnotherProperty
}).ToArray() //or DefaultIfEmpty
});

i had the similar problem while trying to concatenate or union multi sets of navigation properties into single IEnumerable, here is the code sample :
var requiredDocuments =
(from x in db.RequestTypes where (some condition) select x.RequiredDocuments)
.SelectMany(r => r).ToList<DataModel.RequiredDocument>()
.Concat(
(from c in db.Categories where (some condition) select c.RequiredDocuments)
.SelectMany(r => r).ToList<DataModel.RequiredDocument>()
)
.Concat(
(from f in db.Fields where (some condition) select f.RequiredDocuments)
.SelectMany(r => r).ToList<DataModel.RequiredDocument>()
);

If I understand correctly what you are trying to do, I have run into the same problem several times. The bottom line is, doing unions with nested projections is not supported and if you need to do that, you'll have to materialize the results with ToList first.

Related

Update class field in LINQ - is it possible?

I have 2 tables, and want to get records from 1 table and to "update" one of its fields from another table, and to pass final list of "Payment" objects somewhere. I cannot use anonymouse type, i need to get the list of proper typed objects.
There was a long way.
Got data:
var paymentsToShow = from p in paymentsRepository.Payments
join r in recordTypeRepository.RecordType
on p.RecordType equals r.Reference into p_r
where p.Customer == CustomerRef
from r in p_r.DefaultIfEmpty()
select new
{
Payment = p,
RecordType = r
};
var objList = paymentsToShow.ToList();
Change required field (basically, Payment.RecordTypeName is empty):
foreach (var obj in objList)
{
obj.Payment.RecordTypeName = obj.RecordType.Name;
}
Got list with correct type:
var paymentsList = from o in objList
select o.Payment;
Is there any way to get code shorter, to make required field update in the query or something else? I dont know where to look for.
I cannot change database.
You could do it like this:
var paymentsToShow = (from p in paymentsRepository.Payments
join r in recordTypeRepository.RecordType
on p.RecordType equals r.Reference into p_r
where p.Customer == CustomerRef
from r in p_r.DefaultIfEmpty()
select new
{
Payment = p,
RecordType = r
}).Select(x =>
{
x.Payment.RecordTypeName = x.RecordType.Name;
return x.Payment;
});
This will result in an IEnumerable<Payment>, so no anonymous type used.

After grouping by, can I refer to the elements of the original IEnumerable in a LINQ query?

Example:
from OriginalObject in ListOfOriginalObjects
group new CustomObject {
X = OriginalObject.A,
Y = OriginalObject.B
} by OriginalObject.Z into grouping
select new GroupOfCustomObjects {
Z = grouping.Key,
C = OriginalObject.C,
group = grouping
}
In the select part of the query, I'd like to add a property (OriginalObject.C) to the type GroupOfCustomObjects. But it seems that OriginalObject is out of scope in that part of the query. I can sort of understand why, since I am not grouping on that property and I am also not making that property part of CustomObject that I'm grouping.
One workaround is to add a property C to CustomObject and the in the GroupOfCustomObjects read the value of the first CustomObject in the grouping. My issue with that is that I'm adding a property to an object that doesn't need it (CustomObject), just to be able to add it to the GroupOfCustomObjects.
I hope I have explained this properly!
Is there a way to refer to the OriginalObject that the query starts with?
Thanks!
The into clause wipes clean the scope. OriginalObject is removed from scope at that point.
Try it this way:
from OriginalObject in ListOfOriginalObjects
group OriginalObject by OriginalObject.Z into grouping
select new GroupOfCustomObjects {
Z = grouping.Key,
C = grouping.First().C,
group = grouping.Select(x =>
new CustomObject {
X = x.A,
Y = x.B
}
}

Linq - How to query specific columns and return a lists

I am trying to write a linq query that will only return certain columns from my entity object into a list object.
Below is my code which produces an error(can't implicitly convert a generic list of anonymous types to a generic list of type TBLPROMOTION):
IQueryable<TBLPROMOTION> matches = webStoreContext.TBLPROMOTION.Include("TBLSTORE").Include("LKPROMOTIONTYPE");
List<TBLPROMOTION> promotionInfo = null;
promotionInfo = (from p in matches
orderby p.PROMOTION_NM descending
select new { p.EFFECTIVE_DT, p.EXPIRE_DT, p.IS_ACTIVE,
p.PROMOTION_DESC, p.PROMOTION_ID, p.PROMOTION_NM }).ToList();
What would be the best way to accomplish this. I do not want to do a "select p" in this case and return all the columns associated with the query.
thanks in advance,
Billy
Can't you do var promotionInfo = () and get a list of anonymous types?
Okay, basically you can not cast an Anonymous type to a known type like TBLPROMOTION.
ofcourse, you can say var promotionInfo = and then get an IEnumerable<{Anonymoustype}> and use that to do, what you were wanting to do with promotionInfo.
Also, personally I prefer the Fluent version of a linq query, easy on the eyes, good programming diet, at least for me :)
var promotionInfo = matches
.OrderByDescending( p => p.PROMOTION_NM)
.Select( p => new { p.EFFECTIVE_DT,
p.EXPIRE_DT,
p.IS_ACTIVE,
p.PROMOTION_DESC,
p.PROMOTION_ID,
p.PROMOTION_NM})
.ToList();
If you're moving from a L2E query to a Type already defined, you may need a step between. I haven't tried to compile this but something like:
List<TBLPROMOTION> promotions = new List<TBLPROMOTION>();
var results = from p in matches
orderby p.PROMOTION_NM descending
select new
{
p.EFFECTIVE_DT,
p.EXPIRE_DT,
p.IS_ACTIVE,
p.PROMOTION_DESC,
p.PROMOTION_ID,
p.PROMOTION_NM
};
foreach (var v in results)
{
promotions.Add(new TBLPROMOTION(v.EFFECTIVE_DT, v.EXPIRE_DT, v.IS_ACTIVE,
v.PROMOTION_DESC, v.PROMOTION_ID, v.PROMOTION_NM));
}
Based on the comment below, you might try something like:
foreach(var v in results)
{
TBLPROMOTION temp = new TBLPROMOTION();
temp.EFFECTIVE_DT = v.EFFECTIVE_DT;
temp.EXPIRE_DT = v.EXPIRE_DT;
temp.IS_ACTIVE = v.IS_ACTIVE
// Assign Other Properties
promotions.Add(temp);
}
.......
Sorry: Just read the addition to the top.
Are you sure that none of the fields you're leaving out (instead of saying "select p") are required for a TBLPROMOTION object? Also, sense your TBLPROMOTION object is going to have properties (and therefore memory allocated) for those skipped fields, why not just use an annonymous type or set up a helper class that contains only your needed properties?
#Billy, following code worked for me.
List<TBLPROMOTION> promotionInfo =
(from p in matches
orderby p.PROMOTION_NM descending
select new TBLPROMOTION(p.EFFECTIVE_DT, p.EXPIRE_DT, p.IS_ACTIVE,
p.PROMOTION_DESC, p.PROMOTION_ID, p.PROMOTION_NM)
).ToList();
did you try
select new TBLPROMOTION {.....
instead of
select new {.....
List<TBLPROMOTION> promotionInfo = null;
promotionInfo = (from p in matches
orderby p.PROMOTION_NM descending
select new TBLPROMOTION { COL1 = p.EFFECTIVE_DT, COL2 = p.EXPIRE_DT, COL3 = p.IS_ACTIVE... }).ToList();
Where COL1, COL2, ... are the names of the properties on TBLPROMOTION you wish you populate.
If you want a subset of the table you have 2 options:
#Fredou mentioned select new TBLPROMOTION{...}
other way is to create a custom DTO which has the exact properties & select them instead like:
List promotionInfo = ...
select new TBLPROMOTION_DTO{
Effective_dt = ...
}
HTH

Is there a better way to code this LINQ fragment?

I have this fragment of code:
SmsDataClassesDataContext dc = new SmsDataClassesDataContext();
// Get the customer
Customer currentCustomer = dc.Customers.Single( c => c.Hash1 == forThisHash );
// Get from Name (LINQ to XML)
var q = from c in thisSmsPack.Descendants("from")
select c;
string from = q.First().Value;
foreach ( XElement element in thisSmsPack.Descendants("to") )
{
// Create the queue
SmsQueue sq = new SmsQueue();
sq.CustomerId = currentCustomer.CustomerId;
sq.MsgFrom = from;
sq.MsgTo = element.Attribute("name").Value;
sq.MsgPhone = element.Attribute("phone").Value;
sq.MsgBody = element.Attribute("msg").Value;
sq.Priority = currentCustomer.SendsSmsAtPriority;
sq.DontSendUntil = GetNextSendDate();
// sq.TimeCreated = System.DateTime.Now;
currentCustomer.SmsQueues.Add(sq);
}
dc.SubmitChanges();
I am creating new instances of "SmsQueues", populating the values and when the foreach loop is finished I submit the changes. Given the new lambda/linq/anonymous types that .NET 3.5 has, is there a more "modern" way to accomplish the above?
As a side question, maybe related, can I return an existing type composed of different columns in the select part of the linq expression?
Suppose you have three tables:
T1 == T1.Id, T1.Name
T2 == T2.Id, T2.Phone
T3 == T3.Name, T3.Phone, T3.SomethingElse
Can I perform a LINQ query that returns:
T1.Name, T2.Phone, SomethingElseNew
And let .NET know that that is of Type T3 (and it's a new instance of it)?
That way when I SubmitChanges, new T3 instances are inserted in the DB?
I don't know if I make myself clear :S
I don't have a system available to test this, but I think this (or something very close) should work.
CustomerId = currentCustomer.CustomerId;
var sqrange = from element in thisSmsPack.Descendants("to") )
select new SmsQueue
{
// Create the queue
MsgFrom = from,
MsgTo = element.Attribute("name").Value,
MsgPhone = element.Attribute("phone").Value,
MsgBody = element.Attribute("msg").Value,
Priority = currentCustomer.SendsSmsAtPriority,
DontSendUntil = GetNextSendDate()
// TimeCreated = System.DateTime.Now
};
currentCustomer.SmsQueues.AddRange(sqrange);
EDIT: Fixed the numerous syntax errors (as delineated in the comments)
You could do something like this (syntax may be off slightly, no intellisense here):
var q = T1.Join(T2, t => t.Id, t2 => t2.Id)
select new T3{Name=t.Name,Phone=t2.Phone,SomethingElseNew="Chickens"};

How can I get my orderby to work using an anonymous type?

What do I put in my order by?? I want to order by Name. I have moved the orderby after the distinct because I read that it needs to be done last.
var result = (from r in db.RecordDocs
where r.RecordID == recordID
select new
{
DocTypeID = r.Document.DocType.DocTypeID,
Name = r.Document.DocType.Name,
Number = r.Document.DocType.Number
}
).Distinct().OrderBy( );
Just do
.OrderBy(doc => doc.Name)
Another option, if you really prefer the query expression syntax would be to chain your query construction across multiple statements:
var query = from r in db.RecordDocs
where r.RecordID == recordID
select new
{
DocTypeID = r.Document.DocType.DocTypeID,
Name = r.Document.DocType.Name,
Number = r.Document.DocType.Number
};
query = query.Disctinct();
query = from doc in query orderby doc.Name select doc;
Since all of these methods are deferred, this will result in the exact same execution performance.

Resources