Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expression is missing an 'as' clause #403

Closed
Huangzc88 opened this issue Aug 5, 2020 · 11 comments
Closed

Expression is missing an 'as' clause #403

Huangzc88 opened this issue Aug 5, 2020 · 11 comments
Assignees
Labels

Comments

@Huangzc88
Copy link

Huangzc88 commented Aug 5, 2020

var data = new List<object> {
                new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO01" } ,
                new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO02" } ,
                new { ItemCode = "AAAA", Flag = false, SoNo="aaa",JobNo="JNO03" } ,
                new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO04" },
                new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO05" } ,
                new { ItemCode = "BBBB", Flag = true, SoNo="ccc",JobNo="JNO06" } ,
            };
var jsonString = JsonConvert.SerializeObject(data);
var list = JsonConvert.DeserializeObject<List<Dictionary<string,object>>>(jsonString);
var groupList = list.AsQueryable().GroupBy("new (ItemCode,Flag)");

When I execute the code above, it throw an exception with message: 'Expression is missing an 'as' clause'.
Also, if I switch to dynamic types, i have the same problem.

var list = JsonConvert.DeserializeObject<List<dynamic>>(jsonString);

When I use a single field to group, the results sorted by dynamic types don't feel right, and the dictionary types come out as expected.

@JonathanMagnan JonathanMagnan self-assigned this Aug 5, 2020
@JonathanMagnan
Copy link
Member

Hello @Huangzc88 ,

Thank you for reporting,

We will look if there is something we can do with your scenario.

Best Regards,

Jon


Performance Libraries
context.BulkInsert(list, options => options.BatchSize = 1000);
Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper Plus

Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval FunctionSQL Eval Function

@StefH
Copy link
Collaborator

StefH commented Aug 5, 2020

@Huangzc88
Changing the c# code to

var groupList = list.AsQueryable().GroupBy("new (ItemCode as ItemCode, Flag as Flag)");

does not throw any exception.

Is the result what you expect?

JonathanMagnan added a commit that referenced this issue Aug 6, 2020
@JonathanMagnan
Copy link
Member

Hello @Huangzc88 ,

Starting from the v1.2.1, the alias will automatically be added when a single property/string constant is used like in your scenario.

So writing .GroupBy("new (ItemCode,Flag)"); is now valid

Best Regards,

Jon

@Huangzc88
Copy link
Author

Huangzc88 commented Aug 9, 2020

@Huangzc88
Changing the c# code to

var groupList = list.AsQueryable().GroupBy("new (ItemCode as ItemCode, Flag as Flag)");

does not throw any exception.

Is the result what you expect?

@StefH
Thank you for your answer. It's okay to try, but it feels very strange.

@Huangzc88
Copy link
Author

Huangzc88 commented Aug 9, 2020

Hello @Huangzc88 ,

Starting from the v1.2.1, the alias will automatically be added when a single property/string constant is used like in your scenario.

So writing .GroupBy("new (ItemCode,Flag)"); is now valid

Best Regards,

Jon

Hello @Huangzc88 ,

Starting from the v1.2.1, the alias will automatically be added when a single property/string constant is used like in your scenario.

So writing .GroupBy("new (ItemCode,Flag)"); is now valid

Best Regards,

Jon

@JonathanMagnan

Thank you for your answer, thank you for solving this problem so quickly, I will retest.

@Huangzc88 Huangzc88 reopened this Aug 9, 2020
@Huangzc88
Copy link
Author

Huangzc88 commented Aug 9, 2020

Hello @JonathanMagnan

I've retested it and it's really working, and thank you very much for your quick resolution of the issue.
I found another problem that might be optimized.

 var data = new List<object>
            {
                new {ItemCode = "AAAA", Flag = true, SoNo = "aaa", JobNo = "JNO01", Qty = 10},
                new {ItemCode = "AAAA", Flag = true, SoNo = "aaa", JobNo = "JNO02", Qty = 10},
                new {ItemCode = "AAAA", Flag = false, SoNo = "aaa", JobNo = "JNO03", Qty = 10},
                new {ItemCode = "BBBB", Flag = true, SoNo = "bbb", JobNo = "JNO04", Qty = 10},
                new {ItemCode = "BBBB", Flag = true, SoNo = "bbb", JobNo = "JNO05", Qty = 10},
                new {ItemCode = "BBBB", Flag = true, SoNo = "ccc", JobNo = "JNO06", Qty = 10},
            };
var jsonString = JsonConvert.SerializeObject(data);
var list = JsonConvert.DeserializeObject<List<MyDynamicClass>>(jsonString);
var groupList = await list.AsQueryable().GroupBy("new {ItemCode,Flag}").Select("new { Key.ItemCode,Key.Flag,Count() as TotalCount, Sum(Qty) as TotalQty}").ToDynamicListAsync();

MyDynamicClass is inherited from DynamicClass, throw excption :“No applicable aggregate method 'Sum(Object)' exists”,it seems that Qty is treated as an object type and does not automatically identify as an int type.
When I change to the code below, execution is normal:

var groupList = await list.AsQueryable().GroupBy("new {ItemCode,Flag}").Select("new { Key.ItemCode,Key.Flag,Count() as TotalCount, Sum(Convert.ToInt32(Qty)) as TotalQty}").ToDynamicListAsync();

Can we optimize here? Thank you so much.

@JonathanMagnan
Copy link
Member

Hello @Huangzc88 ,

Thank you, we will try it and fix it if we can.

@JonathanMagnan
Copy link
Member

Hello @Huangzc88 ,

Sorry for the delay,

My developer tried it but didn't succeed to reproduce it.

Do you think you could provide him a full runnable example with the case? That will help him to get started and try to fix it.

@Huangzc88
Copy link
Author

Huangzc88 commented Aug 29, 2020

Hello @JonathanMagnan

Thank you for your reply.

Below is all the code that can be executed directly to the console program.:

internal class Program
{
        private static async Task Main(string[] args)
        {
            var data = new List<object>
            {
                new {ItemCode = "AAAA", Flag = true, SoNo = "aaa", JobNo = "JNO01", Qty = 10},
                new {ItemCode = "AAAA", Flag = true, SoNo = "aaa", JobNo = "JNO02", Qty = 10},
                new {ItemCode = "AAAA", Flag = false, SoNo = "aaa", JobNo = "JNO03", Qty = 10},
                new {ItemCode = "BBBB", Flag = true, SoNo = "bbb", JobNo = "JNO04", Qty = 10},
                new {ItemCode = "BBBB", Flag = true, SoNo = "bbb", JobNo = "JNO05", Qty = 10},
                new {ItemCode = "BBBB", Flag = true, SoNo = "ccc", JobNo = "JNO06", Qty = 10},
            };

            var jsonString = JsonConvert.SerializeObject(data);

            var list = JsonConvert.DeserializeObject<List<MyDynamicClass>>(jsonString);

            var groupList = await list.AsQueryable().GroupBy("new {ItemCode,Flag}")
                .Select("new { Key.ItemCode,Key.Flag,Count() as TotalCount, Sum(Qty) as TotalQty}")
                .ToDynamicListAsync();

            Console.WriteLine(groupList.First()["ItemCode"]);
            Console.ReadKey();
        }
 }

public class MyDynamicClass : DynamicClass
{
}

@JonathanMagnan
Copy link
Member

Hello @Huangzc88 ,

Unfortunately, we looked at it and I don't think we will support it at this moment.

The Qty is of type object, and a Sum doesn't exist on this type. Since we compile, we cannot know which is the type at compile time.

So for now, the Convert will still be required to make this example work.

@kapilbhavsar
Copy link

With version 1.2.23 I'm getting this error Expression is missing an 'as' clause'
while i'm trying to parse this expression
string expression = "lst.GroupBy(new (ProductName , StoreName)).Select(it.OrderByDescending(CreatedOn).First())"
but it works with aliasing.
string expression = "lst.GroupBy(new (ProductName as ProductName , StoreName as StoreName)).Select(it.OrderByDescending(CreatedOn).First())"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants