C# 中 IQueryable 的引用问题

发布于 2024-12-05 01:51:47 字数 3999 浏览 0 评论 0原文

代码:

public static IQueryable GetAllBy_HesapAdiFirmaAdiCariNo_Turu_bypage_short(string filter, string tip, int page_size, int page_index, string sortcolumn, bool sortdirection)
{
    //bool tip: 0-tümü, 1-son kullanici, 2-bayi, 3- tedarikçi
    VeriyazDBDataContext db = new VeriyazDBDataContext(); db.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
    var cariKayitlari = DAL.DAOCari.SelectAll();
    var kayitlar = (from rows in cariKayitlari
                    where rows.HESAPADI.ToLower().Contains(filter.ToLower()) ||
                        (rows.CARITURU == "Bireysel" ? rows.B_ADSOYAD.ToLower().Contains(filter.ToLower()) : 
                                                        rows.K_FIRMAADI.ToLower().Contains(filter.ToLower())) ||
                        (rows.CARITURU == "Bireysel" ?
                        rows.B_ADSOYAD.ToLower().Contains(filter.ToUpper()) :
                        rows.K_FIRMAADI.ToLower().Contains(filter.ToUpper())) ||                                   
                        rows.ID.ToString().Contains(filter)
                    select rows);

    switch (tip)
    {
        case ("Tümü"):
        default:
        {
            break;
        }
        case ("Son Kullanici"):
        {
            kayitlar = kayitlar.Where(rows => rows.SONKULLANICI == true);
            break;
        }
        case ("Bayi"):
        {
            kayitlar = kayitlar.Where(rows => rows.BAYI == true);
            break;
        }
        case ("Tedarikçi"):
        {
            kayitlar = kayitlar.Where(rows => rows.TEDARIKCI == true);
            break;
        }
    }

    var kayitlar2 = from rows in kayitlar
                    select new
                    {
                        HESAPNO = rows.ID,
                        HESAPADI = rows.HESAPADI,
                        CARIADI = (rows.CARITURU == "Bireysel" ? rows.B_ADSOYAD : rows.K_FIRMAADI)
                        //BAKIYE= "",
                        //PARABIRIMI = rows.LISTEPARABIRIMI
                    };

    if (sortcolumn != "")
    {
        if (sortdirection)
            kayitlar2 = kayitlar2.Order(sortcolumn, SortDirection.Ascending).AsQueryable();
        else
            kayitlar2 = kayitlar2.Order(sortcolumn, SortDirection.Descending).AsQueryable();
    }

    return kayitlar2.Skip(page_size * (page_index)).Take(page_size);
}

YeniBilet中从两个不同的 System.Web.UI.Page 类调用此方法

  1. 我在下面的方法CariHesaplar


private void cari_paging_button_yenile()
{
    Button_CariIleri.Enabled = true;
    Button_CariGeri.Enabled = true;

    if (CariPageIndex == 0)
        Button_CariGeri.Enabled = false;
    string tumu = "Tümü";

    if (DAL.DAOCari.GetAllBy_HesapAdiFirmaAdiCariNo_Turu_bypage_short(TextBox_CariArama.Text, tumu, CariPageSize, CariPageIndex + 1, CariSortColumn, CariSortDirection).Count() == 0)//problem occured in this row
        Button_CariIleri.Enabled = false;
}

问题:当我在 CariHesaplar 中调用 GetAllBy_HesapAdiFirmaAdiCariNo_Turu_bypage_short 时,我可以在 cari_paging_button_yenile() 的 if 语句中获取计数。但是,在 YeniBilet 中,当我尝试获取计数时() 我正在得到

错误 19“System.Linq.IQueryable”不包含以下定义 “Count”且没有扩展方法“Count”接受第一个参数 可以找到类型“System.Linq.IQueryable”(您是否缺少使用 指令还是程序集引用?)C:\Users\yigit\Desktop\PROTicari (11.04.2011)\UI\MusteriDestek\YeniBilet.aspx.cs 257 17 UI

我尝试了很多方法来解决这个问题,但没有任何效果。后来,我认为这可能是开头缺少 using 语句我检查了这两个类 using 唯一不同的是 using System.Linq.Dynamic; 但它们都有using System.Linq; 所以我认为这不可能是问题。但后来我在 YeniBilet 上写了 using System.Linq.Dynamic; ,它也给出了错误错误消失了。

我的问题是,当页面已经有 using System.Linq; 不是 Linq.Dynamic< 时,为什么不添加 using System.Linq.Dynamic; 引用/code> 就像 Linq 的子集?为什么我必须再次添加它的引用?

Code:

public static IQueryable GetAllBy_HesapAdiFirmaAdiCariNo_Turu_bypage_short(string filter, string tip, int page_size, int page_index, string sortcolumn, bool sortdirection)
{
    //bool tip: 0-tümü, 1-son kullanici, 2-bayi, 3- tedarikçi
    VeriyazDBDataContext db = new VeriyazDBDataContext(); db.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
    var cariKayitlari = DAL.DAOCari.SelectAll();
    var kayitlar = (from rows in cariKayitlari
                    where rows.HESAPADI.ToLower().Contains(filter.ToLower()) ||
                        (rows.CARITURU == "Bireysel" ? rows.B_ADSOYAD.ToLower().Contains(filter.ToLower()) : 
                                                        rows.K_FIRMAADI.ToLower().Contains(filter.ToLower())) ||
                        (rows.CARITURU == "Bireysel" ?
                        rows.B_ADSOYAD.ToLower().Contains(filter.ToUpper()) :
                        rows.K_FIRMAADI.ToLower().Contains(filter.ToUpper())) ||                                   
                        rows.ID.ToString().Contains(filter)
                    select rows);

    switch (tip)
    {
        case ("Tümü"):
        default:
        {
            break;
        }
        case ("Son Kullanici"):
        {
            kayitlar = kayitlar.Where(rows => rows.SONKULLANICI == true);
            break;
        }
        case ("Bayi"):
        {
            kayitlar = kayitlar.Where(rows => rows.BAYI == true);
            break;
        }
        case ("Tedarikçi"):
        {
            kayitlar = kayitlar.Where(rows => rows.TEDARIKCI == true);
            break;
        }
    }

    var kayitlar2 = from rows in kayitlar
                    select new
                    {
                        HESAPNO = rows.ID,
                        HESAPADI = rows.HESAPADI,
                        CARIADI = (rows.CARITURU == "Bireysel" ? rows.B_ADSOYAD : rows.K_FIRMAADI)
                        //BAKIYE= "",
                        //PARABIRIMI = rows.LISTEPARABIRIMI
                    };

    if (sortcolumn != "")
    {
        if (sortdirection)
            kayitlar2 = kayitlar2.Order(sortcolumn, SortDirection.Ascending).AsQueryable();
        else
            kayitlar2 = kayitlar2.Order(sortcolumn, SortDirection.Descending).AsQueryable();
    }

    return kayitlar2.Skip(page_size * (page_index)).Take(page_size);
}

I was calling this method from two different System.Web.UI.Page class in the below method

  1. CariHesaplar
  2. YeniBilet.

private void cari_paging_button_yenile()
{
    Button_CariIleri.Enabled = true;
    Button_CariGeri.Enabled = true;

    if (CariPageIndex == 0)
        Button_CariGeri.Enabled = false;
    string tumu = "Tümü";

    if (DAL.DAOCari.GetAllBy_HesapAdiFirmaAdiCariNo_Turu_bypage_short(TextBox_CariArama.Text, tumu, CariPageSize, CariPageIndex + 1, CariSortColumn, CariSortDirection).Count() == 0)//problem occured in this row
        Button_CariIleri.Enabled = false;
}

Problem: When I call GetAllBy_HesapAdiFirmaAdiCariNo_Turu_bypage_short in CariHesaplar I could get the count in the if statement in cari_paging_button_yenile().However, in YeniBilet when I try to get the Count() I was getting

Error 19 'System.Linq.IQueryable' does not contain a definition for
'Count' and no extension method 'Count' accepting a first argument of
type 'System.Linq.IQueryable' could be found (are you missing a using
directive or an assembly reference?) C:\Users\yigit\Desktop\PROTicari
(11.04.2011)\UI\MusteriDestek\YeniBilet.aspx.cs 257 17 UI

I tried many things to solve this but nothing worked.Afterwards, I thought it could be a missing using statement on the beginning of the page.I checked both of the classes usings only different was using System.Linq.Dynamic; but they both had using System.Linq; so I thought it couldn't be the problem.But then I wrote using System.Linq.Dynamic; on YeniBilet which was giving the error, too and error disappeared.

My question is why is using System.Linq.Dynamic; reference is not added when a page already has using System.Linq; isn't Linq.Dynamic like a subset of Linq?Why do I have to add it's reference again?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若相惜即相离 2024-12-12 01:51:47

Count() 对于 IQueryable 不存在,但对于 IQueryable 存在。除了 System.Linq.Dynamic* 之外,但 System.Linq.Dynamic 不是基类库的一部分,您始终必须添加对它的引用(如果您将其编译到 DLL 中)或使用命名空间(如果您在您的项目中包含 Dynamic.cs)。它不是 BCL 中 Linq 的子集。为了强化大家所说的,使用命名空间仅使用命名空间(其顶级)。它是 C# 语言的设计并由编译器强制执行。

*现已弃用,取而代之的是 System.Linq.Dynamic.Core

Count() does not exist for IQueryable, but does for IQueryable<TSource>. Except in System.Linq.Dynamic*, but System.Linq.Dynamic is not part of the Base Class Library and you will always have to add a reference to it (if you compiled it into a DLL) or use the namespace (if you included Dynamic.cs in your project). Its not a subset of Linq in the BCL. And to reinforce what everyone has stated, using a namespace uses that namespace only (its top-level). It is the design of the C# language and enforced by the compiler.

*Now deprecated in favour of System.Linq.Dynamic.Core

木槿暧夏七纪年 2024-12-12 01:51:47

我的问题是为什么使用 System.Linq.Dynamic;参考不是
当页面已经使用 System.Linq 时添加;不是 Linq.Dynamic
像 Linq 的子集?为什么我必须再次添加它的引用?

这是 C# 中的设计,您谈论的使用方式就是 Java 中导入的工作方式。在 C# 中,没有 using System.Linq.*;

您确定没有使用 IQueryable.Count 而不是 IQueryable.Count()

My question is why is using System.Linq.Dynamic; reference is not
added when a page already has using System.Linq; isn't Linq.Dynamic
like a subset of Linq?Why do I have to add it's reference again?

This is by design in C# the way you're talking about usings is the way imports work in java. In C# there's no using System.Linq.*;

Are you sure you're not using IQueryable.Count instead of IQueryable.Count()?

爱冒险 2024-12-12 01:51:47

Linq.Dynamic 不是 Linq 的子集吗?为什么我必须再次添加它的引用?

那么,这就像问为什么我需要添加 System.Foo;(如果我已经添加 using System;)一样。毕竟 Foo 也是 System 的子集。这是命名空间在 C# 中的工作方式,当您使用 using 指令添加父命名空间时,不会添加嵌套命名空间。

您可能会争论为什么代码文件模板不默认添加 System.Linq.Dynamic。在大多数情况下,这是不必要的,因为 System.Linq.Dynamic语言集成查询功能的专门用途,它使您能够构建动态查询。

关于您遇到的错误,如果您检查 MSDN,IQueryable.Count 扩展方法 System.Linq 的一部分,所以我不确定如何添加 System.Linq.Dynamic 解决了您的问题。 Queryable.Count 方法 (IQueryable)

isn't Linq.Dynamic like a subset of Linq?Why do I have to add it's reference again?

Well that would be the same as asking why do I need to add System.Foo; if I am already adding using System;. After all Foo is also a subset of System. It is the way namespaces work in C#, nested namespaces are not added when you add a parent namespace with a using directive.

You could argue that why doesn't the code file template add by default System.Linq.Dynamic. It would be in most cases unecessary as System.Linq.Dynamic is a specialized use of the Language Integrated Query feature, which enables you to build dynamic queries.

Concerning there error you are getting, if you check MSDN, IQueryable.Count extension method is part of System.Linq so I am not sure how adding System.Linq.Dynamic solved your problem. Queryable.Count Method (IQueryable)

长伴 2024-12-12 01:51:47

count 方法是一种扩展方法。扩展方法需要使用 using 导入定义它们的命名空间

The count method is an extension method. Extension method requires the namespace in which they are defined to be imported with using

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文