linq 里面直接用 var q = from c in db.Customers from o in c.Orders where c.City == "London" select o;代替joinhttp://www.cnblogs.com/Mayvar/archive/2011/07/04/wanghonghua_20110704.html
select new { c.Customer, item.Product, item.Quantity, item.Price, item.Amount, }.ToString(); 为什么要ToString()?
试一试: var query = from c in ctx.ClearingRecord join item in ctx.ClearingRecordItem on c.Id equals item.ClearingRecordId where c.ClearingDate >= dateBegin && c.ClearingDate<= dateEnd select new { c.Customer, item.Product, item.Quantity, item.Price, item.Amount, } 我的多表查询的代码,没有问题啊:var query = from oi in context.OrderItems join o in context.Orders on oi.OrderId equals o.OrderId join p in context.Products on oi.ProductId equals p.ProductId where p.ProductId == 310 select o;
去掉红色部分看看 select new { c.Customer, item.Product, item.Quantity, item.Price, item.Amount, }.ToString();
SQL 用的 FramworkEntity 模型搞的
还是没用,估计是返回值问题, 因为是多表查询 我返回值是做的一个 类 public class ProductListModel {
public string Product { get; set; } public string Quantity { get; set; } public string Price { get; set; } public string Amount { get; set; } }
public List< ProductListModel> GetProductList( DateTime dateBegin, DateTime dateEnd ,String productName) { dateBegin = DateTime.Parse(dateBegin.ToString("yyyy-MM-dd 00:00:00")); dateEnd = DateTime.Parse(dateEnd.ToString("yyyy-MM-dd 23:59:59")); var query = (from clist in ctx.ClearingRecord where (clist.ClearingDate >= dateBegin && clist.ClearingDate<= dateEnd) select clist ).ToList(); return ( from c in query join item in ctx.ClearingRecordItem on c.Id equals item.ClearingRecordId select new { c.Customer, item.Product, item.Quantity, item.Price, item.Amount, }ToList(); }
//db is AdoHelper EntityContainer ec = new EntityContainer(q, db); var mapEntity=ec.Map<ResultClass>((e)=> { e.id=ec.GetItemValue<int>(0); e.CustomerName=ec.GetItemValue<string>(1); e.ClearingDate=ec.GetItemValue<DateTime>(2); e.Amount=ec.GetItemValue<int>(3); e.Price=ec.GetItemValue<Decimal>(4); return e; } ).ToList();详细信息可以参考这篇文章《打造轻量级的实体类数据容器》
LINQ TO SQL 肯定支持join啥var query = from c in (from clist in ctx.ClearingRecord where (clist.ClearingDate >= dateBegin && clist.ClearingDate<= dateEnd) select clist ) join item in ctx.ClearingRecordItem on c.Id equals item.ClearingRecordId select new { c.Customer, item.Product, item.Quantity, item.Price, item.Amount, }
我跟你一样,找了半天就找到你和我差不多 var staffs = from s in staff.Staffs join d in staff.Departments on s.staff_depNumber equals d.dep_number where s.staff_id == 1 select new { s,d }; 一开始我是这样的 提示:不支持方法join; 后来妈的把我惹火了,我就这样了;\ var staffs = from s in staff.Staffs.ToList() join d in staff.Departments.ToList() on s.staff_depNumber equals d.dep_number where s.staff_id == 1 select new { s,d }; 草你妈的 居然成功了。 然后我删除了 第一个ToList() 失败了 还是提示不支持方法 join; 然后我加上一个ToList()删除第二个ToList()也成功了成功了。 数据库字段类型只有int datetime nvarchar(n) 求大神给个解释。
var q =
from c in db.Customers
from o in c.Orders
where c.City == "London"
select o;代替joinhttp://www.cnblogs.com/Mayvar/archive/2011/07/04/wanghonghua_20110704.html
{
c.Customer,
item.Product,
item.Quantity,
item.Price,
item.Amount,
}.ToString();
为什么要ToString()?
var query = from c in ctx.ClearingRecord
join item in ctx.ClearingRecordItem on c.Id equals item.ClearingRecordId
where c.ClearingDate >= dateBegin && c.ClearingDate<= dateEnd
select new
{
c.Customer,
item.Product,
item.Quantity,
item.Price,
item.Amount,
}
我的多表查询的代码,没有问题啊:var query = from oi in context.OrderItems
join o in context.Orders on oi.OrderId equals o.OrderId
join p in context.Products on oi.ProductId equals p.ProductId
where p.ProductId == 310
select o;
select new
{
c.Customer,
item.Product,
item.Quantity,
item.Price,
item.Amount,
}.ToString();
{
public string Product { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
public string Amount { get; set; }
}
{
dateBegin = DateTime.Parse(dateBegin.ToString("yyyy-MM-dd 00:00:00"));
dateEnd = DateTime.Parse(dateEnd.ToString("yyyy-MM-dd 23:59:59"));
var query = (from clist in ctx.ClearingRecord where (clist.ClearingDate >= dateBegin && clist.ClearingDate<= dateEnd) select clist ).ToList();
return ( from c in query join item in ctx.ClearingRecordItem
on c.Id equals item.ClearingRecordId
select new
{
c.Customer,
item.Product,
item.Quantity,
item.Price,
item.Amount,
}ToList();
}
c.CustomerName="XX Name";
c.ClearingDate=DateTime.Now.AddYear(-1);ClearingItem i=new ClearingItem ();OQL q=OQL.From(c)
.InnerJoin(i).On(c.id,i.ClearingRecordId)
.Select(c.id,c.CustomerName,c.ClearingDate,i.Amount,i.Price)
.Where(c.CustomerName,c.ClearingDate)
.End;
//db is AdoHelper
EntityContainer ec = new EntityContainer(q, db);
var mapEntity=ec.Map<ResultClass>((e)=>
{
e.id=ec.GetItemValue<int>(0);
e.CustomerName=ec.GetItemValue<string>(1);
e.ClearingDate=ec.GetItemValue<DateTime>(2);
e.Amount=ec.GetItemValue<int>(3);
e.Price=ec.GetItemValue<Decimal>(4);
return e;
}
).ToList();详细信息可以参考这篇文章《打造轻量级的实体类数据容器》
(from clist in ctx.ClearingRecord
where (clist.ClearingDate >= dateBegin && clist.ClearingDate<= dateEnd)
select clist
)
join item in ctx.ClearingRecordItem
on c.Id equals item.ClearingRecordId
select new
{
c.Customer,
item.Product,
item.Quantity,
item.Price,
item.Amount,
}
var staffs = from s in staff.Staffs
join d in staff.Departments
on s.staff_depNumber equals d.dep_number
where s.staff_id == 1
select new
{
s,d
};
一开始我是这样的 提示:不支持方法join;
后来妈的把我惹火了,我就这样了;\
var staffs = from s in staff.Staffs.ToList()
join d in staff.Departments.ToList()
on s.staff_depNumber equals d.dep_number
where s.staff_id == 1
select new
{
s,d
};
草你妈的 居然成功了。
然后我删除了 第一个ToList() 失败了 还是提示不支持方法 join;
然后我加上一个ToList()删除第二个ToList()也成功了成功了。
数据库字段类型只有int datetime nvarchar(n) 求大神给个解释。