使用来自linq对象List的LINQ编写SQL查询

时间:2012-04-13 06:08:01

标签: sql linq linq-to-sql

我有一个linq对象,我想用linq编写查询。

请帮帮我。

INPUT:

var tags = (from row in tempChildData.AsEnumerable()
                                join tagOrder in tupleInfoDataset.Tables["TagHierarchy"].AsEnumerable() on row.Field<Int64>("TAGID") equals tagOrder.Field<Int64>("TAGID")
                                join tagName in tupleInfoDataset.Tables["SequenceChoiceList"].AsEnumerable() on tagOrder.Field<Int64>("PARENTTAGID") equals tagName.Field<Int64>("TAGID")
                                join facet in tupleInfoDataset.Tables["FacetType"].AsEnumerable() on tagName.Field<string>("Tag_Name") equals facet.Field<string>("Facetname")
                                join tagIdInfo in schDataTogetTagid.AsEnumerable() on row.Field<string>("refTagName").Contains(":") ? row.Field<string>("refTagName").Split(':').Last():row.Field<string>("refTagName") equals tagIdInfo.Field<string>("TAGNAME")
                               where ( childList.Contains(row.Field<Int64>("TAGID")) && facet.Field<string>("FacetType").ToLower().Equals("ctype"))
                               select new
                               {
                                   Tagid = row.Field<Int64>("TAGID"),
                                   TagIdToInsert=tagIdInfo.Field<Int64>("TAGID"),
                                   MaxOccur = row.Field<string>("Maxoccurs"),
                                   MinOccur =Convert.ToInt32(Convert.ToString(row.Field<string>("Minoccur"))),
                                   ParentTagId=tagOrder.Field<Int64>("PARENTTAGID"),
                                   Order=tagOrder.Field<Int64>("TAG_ORDER"),
                                   ParentTagname = tagName.Field<string>("Tag_Name"),
                                   FacetId=facet.Field<Int64>("FacetID")
                               }).ToList();


                    var parentTagID = (from tagIdInfo in tupleInfoDataset.Tables["Tuple"].AsEnumerable()
                                      where tagIdInfo.Field<Int64>("TAGID").Equals(key.Key)
                                      select tagIdInfo.Field<Int64>("ConceptID")).ToList();
                    long parentID =Convert.ToInt64(parentTagID[0]);

现在我希望以上代码中的查询为:

INSERT INTO TUPLE_MAP (TagId,ParentTagId,ParentTagname,MinOccur,MaxOccur,Order)
VALUES (TagIdToInsert,ParentTagId,ParentTagname,MinOccur,MaxOccur,Order)

请帮助我,我不知道如何使用linq编写SQL查询

3 个答案:

答案 0 :(得分:1)

也许是这样的:

using(var db=new DataContext("YourConnectionStringHERE"))
{
  db.TUPLE_MAP.InsertAllOnSubmit(tags.Select (t =>
  new TUPLE_MAP()
  {
       TagId=t.TagIdToInsert,
       ParentTagId=t.ParentTagId,
       ParentTagname=t.ParentTagname,
       MinOccur=t.MinOccur,
       MaxOccur=t.MaxOccur,
       Order=t.Order
  }));
  db.SubmitChanges();
}

或者,如果你想使用parentID,那么就像这样:

using(var db=new DataContext("YourConnectionStringHERE"))
{
  db.TUPLE_MAP.InsertAllOnSubmit(tags.Select (t =>
  new TUPLE_MAP()
  {
       TagId=t.TagIdToInsert,
       ParentTagId=parentID,
       ParentTagname=t.ParentTagname,
       MinOccur=t.MinOccur,
       MaxOccur=t.MaxOccur,
       Order=t.Order
  }));
  db.SubmitChanges();
}

其中db是你的linq数据上下文

有用的参考资料:

修改

因此,如果您正在使用Compact数据库3.5,那么很多东西都是这样的:

using (var conn =new SqlCeConnection("Data Source = test.sdf; Password ='pass'"))
{
        foreach (var tag in tags)
        {
            using(var cmd = conn.CreateCommand())
            {

                cmd.CommandText = @"INSERT INTO TUPLE_MAP (TagId,ParentTagId,ParentTagname,MinOccur,MaxOccur,Order)
                                    VALUES (@TagIdToInsert,@ParentTagId,@ParentTagname,@MinOccur,@MaxOccur,@Order)";

                cmd.Parameters.AddWithValue("@TagIdToInsert", tag.TagIdToInsert);
                cmd.Parameters.AddWithValue("@ParentTagId", tag.ParentTagId);
                cmd.Parameters.AddWithValue("@ParentTagname", tag.ParentTagname);
                cmd.Parameters.AddWithValue("@MinOccur", tag.MinOccur);
                cmd.Parameters.AddWithValue("@MaxOccur", tag.MaxOccur);
                cmd.Parameters.AddWithValue("@Order", tag.Order);

                cmd.ExecuteNonQuery();

            }
        }
    }

有用的参考资料:

答案 1 :(得分:0)

使用linq Pad或sql profiler查看生成的SQL。 您也可以使用visual studio来实现此目的。在调试模式下,将光标放在变量“tags”上,您将能够看到SQL。

答案 2 :(得分:0)

我假设您正在使用Linq to SQL,如果您这样做,您将在xxxDataContext中使用名为Tuple_map的实体。那么你只需要创建像这样的实体的对象....

using (XXXDataContext context = new XXXDataContext())
{
   Tuple_map obj = new Tuple_map();
   //Populate obj properties like obj.tabid = from objects you got it from above query
   context.Tuple_map.InsertOnSubmit(obj);
   context.SubmitChanges();
}