C# 中 IQueryable 的引用问题
代码:
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
类调用此方法
- 我在下面的方法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
- CariHesaplar
- 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 using
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Count()
对于IQueryable
不存在,但对于IQueryable
存在。除了 System.Linq.Dynamic* 之外,但 System.Linq.Dynamic 不是基类库的一部分,您始终必须添加对它的引用(如果您将其编译到 DLL 中)或使用命名空间(如果您在您的项目中包含 Dynamic.cs)。它不是 BCL 中 Linq 的子集。为了强化大家所说的,使用命名空间仅使用该命名空间(其顶级)。它是 C# 语言的设计并由编译器强制执行。*现已弃用,取而代之的是 System.Linq.Dynamic.Core
Count()
does not exist forIQueryable
, but does forIQueryable<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
这是 C# 中的设计,您谈论的使用方式就是 Java 中导入的工作方式。在 C# 中,没有
using System.Linq.*;
您确定没有使用
IQueryable.Count
而不是IQueryable.Count()
?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 ofIQueryable.Count()
?那么,这就像问为什么我需要添加
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)Well that would be the same as asking why do I need to add
System.Foo;
if I am already addingusing System;
. After allFoo
is also a subset ofSystem
. 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 asSystem.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 ofSystem.Linq
so I am not sure how addingSystem.Linq.Dynamic
solved your problem. Queryable.Count Method (IQueryable)count
方法是一种扩展方法。扩展方法需要使用using
导入定义它们的命名空间The
count
method is an extension method. Extension method requires the namespace in which they are defined to be imported withusing