开发者

Reference problem in C# for IQueryable

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().Con开发者_如何学Gotains(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?


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


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()?


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)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜