LINQ to Entity: How to cast a complex type to a DTO - linq

Background:
I have created a function import which is available in my context object as GetJournalViewItemsQuery()
The function import returns a complex type called JournalViewItem.
Now when I try to load the JournalViewItem into my application DTO called JournalEntry I come up with error:
Error 7 Cannot implicitly convert type 'MyApp.Infrastructure.Models.JournalEntry' to 'MyApp.SqlData.JournalViewItem'
This is the code:
var journalEntry = Context.GetJournalViewItemsQuery()
.Where(i => i.JournalItemId == _journalEntryId)
.Select(x => new JournalEntry(x.JournalItemId,
x.HeaderText,x.JournalText, x.LastUpdatedOn,
x.JournalItemTypeId)).Single();
The error happens at the "new JournalEntry" line.
My question: How can I cast the JournalViewItem complex type to my DTO ?
Thank you
After #JanR suggestion I still have the same problem. The modified code is:
var journalEntry = Context.GetJournalViewItemsQuery()
.Where(i => i.JournalItemId == _journalEntryId)
.Select(x => new JournalEntry
{
JournalEntryNumber = x.JournalItemId,
HeaderText = x.HeaderText,
BodyText = x.JournalText,
LastUpdatedOn = x.LastUpdatedOn,
JournalEntryType = x.JournalItemTypeId
}).Single();
I found out the reason for my problem. I failed to mention (my apologies) that I'm working off generated code from WCF RIA Domain Services for Silverlight application. As such the Context.GetJournalViewItemsQuery() needs to be executed and THEN I can query the results on my callback method using the LINQ expression that #Chuck.Net and JanR have suggested.
Here's the working code to those who might be interested:
public IList<JournalEntryHeader> GetJournalEntryHeaders()
{
PerformQuery<JournalViewItem>(Context.GetJournalViewItemsQuery(), GetJournalEntryHeadersFromDbComplete);
return _journalHeaders;
}
void PerformJournalEntryHeadersQuery(EntityQuery<JournalViewItem> qry,
EventHandler<EntityResultsArgs<JournalViewItem>> evt)
{
Context.Load<JournalViewItem>(qry, r =>
{
if (evt != null)
{
try
{
if (r.HasError)
{
evt(this, new EntityResultsArgs<JournalViewItem>(r.Error));
}
else if (r.Entities.Count() > 0)
{
evt(this, new EntityResultsArgs<JournalViewItem>(Context.JournalViewItems));
}
else if (r.Entities.Count() == 0 && _currentJournalItemsPage > 0)
{
GetPrevPageJournalEntryHeadersAsync();
}
}
catch (Exception ex)
{
evt(this, new EntityResultsArgs<JournalViewItem>(ex));
}
}
}, null);
}
void GetJournalEntryHeadersFromDbComplete(object sender, EntityResultsArgs<JournalViewItem> e)
{
if (e.Error != null)
{
string errMsg = e.Error.Message;
}
else
{
_journalHeaders = e.Results
.Select(
x => new JournalEntryHeader(x.JournalItemId,
x.ProjectName,
x.TopicName,
x.HeaderText,
x.EntryTypeName,
x.LastUpdatedOn)).ToList();
GetJournalEntryHeadersComplete(this, new JournalEntryHeaderItemsEventArgs(_journalHeaders));
}
}

What you need to do is the following, in the new JournalEntry() function you will need to set all the properties to the JournalViewItem object.
var journalEntry = Context.GetJournalViewItemsQuery()
.Where(i => i.JournalItemId == _journalEntryId)
.Select(x => new JournalEntry {
JournalEntryId = x.JournalItemId,
HeaderText = x.HeaderText,
JournalText = x.JournalText
//etc
}).Single();
I am just guessing the actual property names here as I am not familiar with what the JounralEntry object looks like.
EDIT: added {}

I created a ConsoleApp to test #JanR answer. It seems to be working correctly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackOverFlowConsoleApplication
{
class Program
{
static void Main(string[] args)
{
List<JournalViewItem> JournalViewItems = new List<JournalViewItem>()
{
new JournalViewItem(){JournalItemId =1, HeaderText="HeaderText", JournalText="JournalText", LastUpdatedOn= DateTime.Today, JournalItemTypeId=1},
};
int _journalEntryId = 1;
var journalEntry = JournalViewItems
.Where(i => i.JournalItemId == _journalEntryId)
.Select(x => new JournalEntry
{
JournalEntryNumber = x.JournalItemId,
HeaderText = x.HeaderText,
BodyText = x.JournalText,
LastUpdatedOn = x.LastUpdatedOn,
JournalEntryType = x.JournalItemTypeId
}).Single();
}
class JournalViewItem
{
public int JournalItemId { get; set; }
public string HeaderText { get; set; }
public string JournalText { get; set; }
public DateTime LastUpdatedOn { get; set; }
public int JournalItemTypeId { get; set; }
}
class JournalEntry
{
public int JournalEntryNumber { get; set; }
public string HeaderText { get; set; }
public string BodyText { get; set; }
public DateTime LastUpdatedOn { get; set; }
public int JournalEntryType { get; set; }
}
}
}

I looked into complx type a little bit. I tried the following code in my own project and was able to reproduce the error you mentioned:
var result = (from y in CS.PSMBIPMTTXLOGs select new PSMBIPMTCONTROL(){MBI_PMT_TX_ID = y.MBI_PMT_TX_ID}).ToList();
But when I changed it to return anonymous type, it worked:
var result = (from y in CS.PSMBIPMTTXLOGs select new {MBI_PMT_TX_ID = y.MBI_PMT_TX_ID}).ToList();
You mentioned that you have even tried creating an anonymous type instead of newing it into my DTO, but it still complains. Can you post your code for returning an anonymous type and the error message it gives? Thank you.

I found out the reason for my problem. I failed to mention (my apologies) that I'm working off generated code from WCF RIA Domain Services for Silverlight application. As such the Context.GetJournalViewItemsQuery() needs to be executed and THEN I can query the results on my callback method using the LINQ expression that #Chuck.Net and JanR have suggested.
You'll find the answer in the original post where I entered the question.

Related

Linq Queryable Expression Tree: SqlException: Incorrect syntax near '&'

I am getting an error: { SqlException: Incorrect syntax near '&' } which I cannot seem to figure out. I have a class QuestionResult (seen below).
public class QuestionResult
{
public int Id { get; set; }
public float? Result { get; set; }
public string ResultText { get; set; }
//Foreign Keys & Connections
public virtual SurveyResult SurveyResult { get; set; }
public virtual Question Question { get; set; }
public int QuestionId { get; set; }
public int SurveyResultId { get; set; }
}
which I wish to filter by QuestionId & Result, for example {QuestionId == 48 && Result >= 24}. The issue appears to be around using Expression.And. I tried this test without using it, using just one of the conditions and it ran fine. I have previously used Expression.And and it has worked.
public static List<int> GetIdList()
{
IQueryable<QuestionResult> questionResultList = _context.QuestionResult.AsQueryable();
var questionResult = Expression.Parameter(typeof(QuestionResult), "qr");
MemberExpression questionId = Expression.Property(questionResult, "QuestionId");
MemberExpression result = Expression.Property(questionResult, "Result");
var conditionA = Expression.Equal(questionId, Expression.Constant(48));
var conditionB = Expression.GreaterThanOrEqual(result, Expression.Convert(Expression.Constant(24), result.Type));
var and = Expression.And(conditionA, conditionB);
var predicateBody = Expression.Lambda<Func<QuestionResult, bool>>(and, questionResult);
MethodCallExpression query = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { questionResultList.ElementType },
questionResultList.Expression,
predicateBody
);
var questionResultIdList = questionResultList.Provider.CreateQuery<QuestionResult>(query).Select(i => i.Id).ToList();
return questionResultIdList;
}
Can you see somewhere that I have gone wrong with this? Debugging the query before it has run shows:
{value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[SurveyV3.Models.DatabaseModels.QuestionResult]).Where(qr => ((qr.QuestionId == 48) And (qr.Result >= Convert(24))))}
which looks right to me. I have been using questions on here and this Microsoft page for assistance https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/how-to-use-expression-trees-to-build-dynamic-queries
Thanks, Oj
var and = Expression.And(conditionA, conditionB);
Expression.And does a bitwise AND operation.
You want Expression.AndAlso which does a logical AND.

How could I read one to many linked table using link with Entity Framework?

I am new to Entity Framework and Linq (Visual Studio 2017 - EF 5.0) . Currently, I could read tables without issue but wonder how could I read a linked table.
My current functions do it but sure there is a simple way than two step reading that I have developed.
public override List<CartItem> GetMyCartOrderItems(int UserID)
{
try
{
using (foodorderingdbEntities oMConnection = new foodorderingdbEntities())
{
var oCart = oMConnection.carts.SingleOrDefault(p => p.USER_ID == UserID);
if (oCartItems != null)
{
int CartID = oCart.CART_ID;
var oCartItems = oMConnection.cart_item.Where(p => p.CART_ITEM_CART_ID == CartID);
if (oCartItems != null)
{
List<CartItem> oRecList = new List<CartItem>();
foreach (cart_item oDBrec in oCartItems)
{
CartItem oRec = new CartItem();
oRec.CartID = oDBrec.CART_ITEM_ID;
oRec.CartItemID = oDBrec.CART_ITEM_CART_ID;
oRec.DateTime = oDBrec.CART_ITEM_ADDED_DATE_TIME;
oRec.SystemComments = oDBrec.CART_ITEM_SYSTEM_COMMENTS;
oRecList.Add(oRec);
}
return oRecList;
}
else { return null; }
}
else { return null; }
}
}
catch (Exception ex)
{
//IBLogger.Write(LOG_OPTION.ERROR, "File : MHCMySQLDataConection.cs, Method : GetPatientByID(1), Exception Occured :" + ex.Message + Environment.NewLine + "Trace :" + ex.StackTrace);
return null;
}
}
You could see that I get Cart ID from Carts table using UserID and then I use the CartID retrieve cart Items from Cart_Item table. Cart_Item_Cart_ID is a foreign key in cart_item table. (This is a one to many table)
This is what I am thinking but obviously does not work.
List<cart_item> oCartItems = oMConnection.carts.SingleOrDefault(c => c.USER_ID == UserID).cart_item.Where(p => p.CART_ITEM_CART_ID = c.CART_ID).ToList<cart_item>();
Any help ?
My entity relation
public partial class cart
{
public cart()
{
this.cart_item = new HashSet<cart_item>();
}
public int CART_ID { get; set; }
public int USER_ID { get; set; }
public decimal ORDER_TOTAL_COST { get; set; }
public virtual ICollection<cart_item> cart_item { get; set; }
public virtual user user { get; set; }
}
Because your query has multiple levels of one to many relationships, and you just want the cart_items, it's easier to go the other way like this:
var oCart = oMConnection.cart_item
.Where(c=>c.cart.user.USER_ID == UserID);
Going the way you did should have worked as well, but you needed to use SelectMany instead of select like this:
var oCartItems = oMConnection.carts
.Where(c=>c.USER_ID==UserID)
.SelectMany(c=>c.cart_item);

Building a query (LINQ) with a sub-query

For simplicity sake lets assume I have the following two classes:
public class ComplexClass
{
public List<SubClass> SubClasses { get; set; }
public string Name { get; set; }
}
public class SubClass
{
public string Name { get; set; }
}
I have a List<ComplexClass> and I need to build a query based on some parameters.
It's an easy task if all I need to do is use the Name property of ComplexClass. Here's an example:
static IQueryable<ComplexClass> GetQuery(string someParameter, string someOtherParameter)
{
var query = list.AsQueryable();
if (!String.IsNullOrEmpty(someParameter))
query = query.Where(c => c.Name.StartsWith(someParameter));
if (!String.IsNullOrEmpty(someOtherParameter))
query = query.Where(c => c.Name.EndsWith(someOtherParameter));
return query;
}
Based on the parameters I have I can add more query elements. Of course the above example is simple, but the actual problem contains more parameters, and that number can grow.
Things aren't as simple if I want to find those ComplexClass instances which have SubClass instances which meet criteria based on parameters:
static IQueryable<ComplexClass> GetSubQuery(string someParameter, string someOtherParameter)
{
var query = list.AsQueryable();
if (!String.IsNullOrEmpty(someParameter))
if (!String.IsNullOrEmpty(someOtherParameter))
return query.Where(c => c.SubClasses.Where(sc => sc.Name.StartsWith(someParameter) && sc.Name.EndsWith(someOtherParameter)).Any());
else
return query.Where(c => c.SubClasses.Where(sc => sc.Name.StartsWith(someParameter)).Any());
else
if (!String.IsNullOrEmpty(someOtherParameter))
return query.Where(c => c.SubClasses.Where(sc => sc.Name.EndsWith(someOtherParameter)).Any());
else
return null;
}
I can no longer just add bits of the query based on each parameter, I now need to write the whole query in one go, and this means I need to check every combination of parameters, which is hardly ideal.
I suspect the key is to build an Expression class and create a lambda expression from that, but I'm not sure how to tackle the problem.
Any suggestions? :)
EDIT:
My initial idea was this:
static IQueryable<ComplexClass> GetSubQuery(string someParameter, string someOtherParameter)
{
var query = list.AsQueryable();
query = query.Where(c =>
{
var subQuery = c.SubClasses.AsQueryable();
if (!String.IsNullOrEmpty(someParameter))
subQuery = subQuery.Where(sc => sc.Name.StartsWith(someParameter));
if (!String.IsNullOrEmpty(someOtherParameter))
subQuery = subQuery.Where(sc => sc.Name.EndsWith(someOtherParameter));
return subQuery.Any();
});
return query;
}
This works in my small console test application as it's using LINQ to Objects. Unfortunately, I need to use Entity Framework and LINQ to Entities, which causes an implementation similar to the one above to throw a A lambda expression with a statement body cannot be converted to an expression tree error message.
I'm assuming that in you real-life code the SubClasses property is IQueryable<SubClass> rather than List<SubClass>?
If so, then your query building becomes easy:
static IQueryable<ComplexClass> GetSubQuery(
string someParameter, string someOtherParameter)
{
var query = list.AsQueryable();
if (!String.IsNullOrEmpty(someParameter))
query = query.Where(c => c.SubClasses
.Where(sc => sc.Name.StartsWith(someParameter)).Any());
if (!String.IsNullOrEmpty(someOtherParameter))
query = query.Where(c => c.SubClasses
.Where(sc => sc.Name.StartsWith(someOtherParameter)).Any());
return query;
}
Mixing IEnumerable<T> and IQueryable<T> using AsQueryable() is never a good idea.
I implemented my solution in a simple Console Project:
internal class Program
{
#region Constants and Fields
private static readonly List<ComplexClass> list = new List<ComplexClass>
{
new ComplexClass
{
Name = "complex",
SubClasses = new List<SubClass>
{
new SubClass
{
SubName = "foobar"
}
}
},
new ComplexClass
{
Name = "complex",
SubClasses = new List<SubClass>
{
new SubClass
{
SubName = "notfoobar"
}
}
}
};
#endregion
#region Public Methods
public static void Main(string[] args)
{
Console.WriteLine("foo / bar :");
GetSubQuery("foo", "bar");
Console.WriteLine();
Console.WriteLine("foo / null :");
GetSubQuery("foo", null);
Console.WriteLine();
Console.WriteLine("string.Empty / bar :");
GetSubQuery(string.Empty, "bar");
Console.WriteLine();
Console.WriteLine("maeh / bar :");
GetSubQuery("maeh", "bar");
Console.ReadKey();
}
#endregion
#region Methods
private static void GetSubQuery(string startsWith,
string endsWith)
{
var query = from item in list
let StartIsNull = string.IsNullOrEmpty(startsWith)
let EndIsNull = string.IsNullOrEmpty(endsWith)
where
(StartIsNull || item.SubClasses.Any(sc => sc.SubName.StartsWith(startsWith)))
&& (EndIsNull || item.SubClasses.Any(sc => sc.SubName.EndsWith(endsWith)))
select item;
foreach (var complexClass in query)
{
Console.WriteLine(complexClass.SubClasses.First().SubName);
}
}
#endregion
public class ComplexClass
{
#region Public Properties
public string Name { get; set; }
public List<SubClass> SubClasses { get; set; }
#endregion
}
public class SubClass
{
#region Public Properties
public string SubName { get; set; }
#endregion
}
}
The Console Output is:
foo / bar :
foobar
foo / null :
foobar
string.Empty / bar :
foobar
notfoobar
maeh / bar :

LINQ to MongoDB: .Any with a Predicate

I have a Collection in MongoDB of S documents. Each S has a collection of UserPermission objects, each of which have a UserId property. I want to select all the S documents that have a UserPermission with a certain UserId:
return collection.Where(s => s.UserPermissions.Any(up => up.UserId == userIdString)).ToList();
I get an error telling me that .Any with a predicate is not supported. The MongoDB docs say: "You can usually rewrite such a query by putting an equivalent where clause before the projection (in which case you can drop the projection)."
What does that mean? Any idea how I would change my query to get around this limitation?
Here's a full example. You can see I've tried 2 different queries and neither is supported:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
namespace MongoSample
{
class Program
{
static void Main(string[] args)
{
App app1 = new App() { Name = "App1", Users = new List<User>()
{
new User() { UserName = "Chris" } }
};
App app2 = new App() { Name = "App2", Users = new List<User>()
{
new User() { UserName = "Chris" },
new User() { UserName = "Carlos" } }
};
MongoServer server = MongoServer.Create();
MongoDatabase database = server.GetDatabase("test");
MongoCollection appCollection = database.GetCollection("app");
appCollection.Insert(app1);
appCollection.Insert(app2);
// Throws "Any with a predicate not supported" error
//var query = appCollection.AsQueryable<App>()
// .Where(a => a.Users.Any(u => u.UserName == "Carlos"));
// Throws "Unsupported Where Clause" error.
var query = appCollection.AsQueryable<App>()
.Where(a => a.Users.Where(u => u.UserName == "Carlos").Any());
foreach (App loadedApp in query)
{
Console.WriteLine(loadedApp.ToJson());
}
Console.ReadLine();
}
}
class App
{
public string Name { get; set; }
public List<User> Users { get; set; }
}
class User
{
public string UserName { get; set; }
}
}
Any() without a predicate is supported, so you can do:
collection.Where(s => s.UserPermissions
.Where(up => up.UserId == userIdString).Any() )
(this is the "equivalent where clause" put before the Any)

Sorting a List of Class with LINQ

I have a List<MyClass> and I want to sort it by DateTime CreateDate attribute of MyClass.
Is that possible with LINQ ?
Thanks
To sort the existing list:
list.Sort((x,y) => x.CreateDate.CompareTo(y.CreateDate));
It is also possible to write a Sort extension method, allowing:
list.Sort(x => x.CreateDate);
for example:
public static class ListExt {
public static void Sort<TSource, TValue>(
this List<TSource> list,
Func<TSource, TValue> selector) {
if (list == null) throw new ArgumentNullException("list");
if (selector == null) throw new ArgumentNullException("selector");
var comparer = Comparer<TValue>.Default;
list.Sort((x,y) => comparer.Compare(selector(x), selector(y)));
}
}
You can enumerate it in sorted order:
IEnumerable<MyClass> result = list.OrderBy(element => element.CreateDate);
You can also use ToList() to convert to a new list and reassign to the original variable:
list = list.OrderBy(element => element.CreateDate).ToList();
This isn't quite the same as sorting the original list because if anyone still has a reference to the old list they won't see the new ordering. If you actually want to sort the original list then you need to use the List<T>.Sort method.
Here is a sample:
using System.Collections.Generic;
using System.Linq;
namespace Demo
{
public class Test
{
public void SortTest()
{
var myList = new List<Item> { new Item { Name = "Test", Id = 1, CreateDate = DateTime.Now.AddYears(-1) }, new Item { Name = "Other", Id = 1, CreateDate = DateTime.Now.AddYears(-2) } };
var result = myList.OrderBy(x => x.CreateDate);
}
}
public class Item
{
public string Name { get; set; }
public int Id { get; set; }
public DateTime CreateDate { get; set; }
}
}
Sure the other answers with .OrderBy() work, but wouldn't you rather make your source item inherit from IComparable and just call .Sort()?
class T {
public DateTime CreatedDate { get; set; }
}
to use:
List<T> ts = new List<T>();
ts.Add(new T { CreatedDate = DateTime.Now });
ts.Add(new T { CreatedDate = DateTime.Now });
ts.Sort((x,y) => DateTime.Compare(x.CreatedDate, y.CreatedDate));

Resources