使用System.Linq.Dynamic(Dymanic LINQ API)时忽略'where'子句的问题 [英] Issues omitting the 'where' clause when using System.Linq.Dynamic (Dymanic LINQ API)

查看:103
本文介绍了使用System.Linq.Dynamic(Dymanic LINQ API)时忽略'where'子句的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此处发布的LINQ动态查询库-http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the- linq-dynamic-query-library.aspx

以下作品很棒:

I''m trying to use the LINQ Dynamic Query Library posted here - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

The following works great:

List<string> paramsList = new List<string> {"CustomerID"};
        var customer =
            ctx.Customers.Where(cus=>cus.CompanyName.Contains("A")).Select("new(" +
                                 string.Join(", ", paramsList.ToArray()) +
                                 ")");



但是,如果我忽略"Where"子句并执行类似的操作



However if I omit the "Where" clause and do something like this

List<string> paramsList = new List<string> {"CustomerID"};
        var customer =
            ctx.Customers.Select("new(" +
                                 string.Join(", ", paramsList.ToArray()) +
                                 ")");



我收到以下错误:



I get the following error:

'new' cannot be resolved into a valid type constructor or function., near function, method or type constructor



我在这里缺少什么?



What am I missing here?

推荐答案

在MSDN论坛上回答了我的问题:

http://goo.gl/KwikB
Got a reply to my question on the MSDN forums:

http://goo.gl/KwikB


我尝试了两个示例,它们都很好.第一个查询返回了75行;第二个返回了91行.我从Scott的页面下载了示例,打开了他的DynamicQuery项目,并如下修改了Main():
I tried both your examples, and they completed fine. The first query returned 75 rows; the second one returned 91 rows. I downloaded samples from Scott''s page, opened his DynamicQuery project, and modified the Main() as follows:
//Copyright (C) Microsoft Corporation.  All rights reserved.

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Linq.Dynamic;
using System.Windows.Forms;
using NorthwindMapping;

namespace Dynamic
{
    class Program
    {
        static void Main(string[] args)
        {
            Northwind db = new Northwind("Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI;"); 
            List<string> paramsList = new List<string> { "CustomerID" };
            var customer =
                db.Customers.
                //Where(cus=>cus.CompanyName.Contains("A")).
                Select("new(" +
                                     string.Join(", ", paramsList.ToArray()) +
                                     ")");
            var count = 0;
            foreach (var x in customer) {
                Console.WriteLine(x);
                count++;
            }
            Console.WriteLine("Returned {0} customers", count);
            Console.ReadLine();
        }
    }
}

此代码运行完整,没有错误,带有Where子句的注释和未注释.

我们是否在运行他的System.Linq.Dynamic的不同版本?我无法找到将产生您在Scott的实现中报告的错误消息的代码.

This code ran to completion with no errors, with Where clause commented and uncommented.

Are we running different versions of his System.Linq.Dynamic? I was unable to locate the code that would produce the error message that you reported in Scott''s implementation.


这篇关于使用System.Linq.Dynamic(Dymanic LINQ API)时忽略'where'子句的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆