2

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

请帮我。

输入:

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 查询

4

3 回答 3

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();
}

或者,如果您想使用parentIDthen 这样的东西:

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

            }
        }
    }

有用的参考资料:

于 2012-04-13T06:42:47.467 回答
0

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

于 2012-04-13T06:15:33.143 回答
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();
}
于 2012-04-13T06:39:33.583 回答